开发者

img src from php file

开发者 https://www.devze.com 2023-03-28 11:34 出处:网络
I have the following code: <html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">

I have the following code:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Image uploader</title>
</head>
<body>
<h2>Image uploader</h2>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
Before Image:
<input type="file" name="before"  size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<br />
<?php echo '<img src="showimage.php?type=before"/>' ?>

<br />
<br />

After Image:
<input type="file" name="after"  size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<br />
<?php echo '<img src="showimage.php?type=after"/>' ?>

<br />
<br />
<input type="sub开发者_JAVA技巧mit" value="submit">
</form>

Here's my showimage:

<?php
   if((is_uploaded_file($_FILES['before']['tmp_name']) && getimagesize($_FILES['before']['tmp_name']) != false) || 
      (is_uploaded_file($_FILES['after']['tmp_name']) && getimagesize($_FILES['after']['tmp_name']) != false))
    {
            header("Content-type: image/jpg");
            if ($_GET['type'] == 'before')
                echo $before_img = fopen($_FILES['before']['tmp_name'], 'rb');
            else ($_GET['type'] == 'after')
                echo $after_img = fopen($_FILES['after']['tmp_name'], 'rb');

    }
   else {
          echo 'http://www.stampinup.net/esuite/images/pages/noImageUploaded.png?763.458';
   }

?>

The issue is that img src won't render the showimage.php.. why is this?


You've got a fundamental misunderstanding of PHP file uploads and file handling in general.

echo $somevar = fopen won't output the image itself. It'll put the handle that fopen returns, which'll be some meaningless integer. What you want is (at minimum):

readfile($_FILES['before']['tmp_name']);

which'll open the file and send its contents to the browser.

You are also not saving the uploaded files anywhere, so basically the user uploads 2 images, you output one, and then the images are deleted - PHP does not save uploaded files permanently. You must manually move/copy the files elsewhere in order to preserve them. Generally that's done with the move_uploaded_file() function

As well, referring to your upload handler script in an <img> tag will also not work. That call will go out as a GET request, and file uploads do NOT occur via GET, so your script will return an empty/broken image because there is no file for it to read.

0

精彩评论

暂无评论...
验证码 换一张
取 消