Jump to content

[SOLVED] Show uploaded image


pcw

Recommended Posts

I am trying to display the image that a user has just uploaded. This seems to do the trick, but i cant work out how to make the <img src point to the members/uploads directory. If I add members/uploads to <img src= it doesnt seem to work :(

 

move_uploaded_file($_FILES["file"]["tmp_name"],

      "members/uploads/" . $_FILES["file"]["name"]);

      echo "<img src=" . $_FILES["file"]["name"] . " />";

Link to comment
https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/
Share on other sites

This is the who script if it helps. Made another change to the <img src= but still no luck

 

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
       if (file_exists("members/uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "members/uploads/" . $_FILES["file"]["name"]);
      $image = ("members/uploads/" . $_FILES["file"]["name"]);
      echo "<img src=$image>"; 
      
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

 

When I check the properties of the red X where the image should be. The URL is shown as

 

http://www.mysite.com/cgi-bin/members/uploads/3.gif

 

which is correct. Why is it not showing the image?

$_FILES['file']['name']

This will be the name of the file as it was taken from the user's computer - you canot use this value to display.

 

$_FILES['file']['tmp_name']

This is the name and path of the file as it is stred in the temporary buffer - you cannot use this either.

 

You need to take the file ($_FILES['file']['tmp_name']) and move it to where you want to store it.

 

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
  //worked OK
  echo '<img src="'.$uploadedfile.'"/>';
} else {
  //move failed
}

Hi Yesideez, thanks for your replies.

 

When I use the script I originally posted, the URL in the properties of the red "X" is correct, and the file exists.

 

I have now got this, which I used from what you posted. But no image or red "X" is displayed. I am not sure if I done it right

 

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
      
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
  //worked OK
  echo '<img src="'.$uploadedfile.'"/>';
   
   if (file_exists("members/uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
} 
}
} else {
  echo "Invalid file";
  }
?>

If you indent your code properly it's a lot easier to read. Try this- I've added some debug info to show you where it's moving the files to and the filename.

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";

    $targetFolder='members/uploads/'
    $uploadfile=$targetFolder.$_FILES['file']['name'];
    //OUR DEBUG INFORMATION
    echo 'UPLOAD TO:'.$uploadFile.'<br>';
    //END DEBUG INFORMATION

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
      //worked OK
      echo '<img src="'.$uploadedfile.'"/>';

      if (file_exists("members/uploads/" . $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
    } else {
      echo 'File not copied.';
    }
  }
} else {
  echo "Invalid file";
}
?>

Hi, I have tried this and it doesnt work.

 

I run a syntax check and get this error:

 

Parse error:  parse error, unexpected T_VARIABLE in upload_file.php on line 12

 

line 12: $uploadfile=$targetFolder.$_FILES['file']['name'];

 

also should

 

    echo '<img src="'.$uploadedfile.'"/>';

 

be

 

    echo '<img src="'.$uploadFile.'"/>'; 

 

?

 

Thanks

the example was provided for free ,

 

a ; was left out that all.

 

as a learner should off seen that.

 

 

You have been given a nice snippet,look up the functions, and learn,

It not passable to learn via getting the results via others all the time.

 

and not fair on the person who provided the snippet.

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";

    $targetFolder='members/uploads/';
    $uploadfile=$targetFolder.$_FILES['file']['name'];
    //OUR DEBUG INFORMATION
    echo 'UPLOAD TO:'.$uploadFile.'<br>';
    //END DEBUG INFORMATION

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
      //worked OK
      echo '<img src="'.$uploadedfile.'"/>';

      if (file_exists("members/uploads/" . $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
    } else {
      echo 'File not copied.';
    }
  }
} else {
  echo "Invalid file";
}
?>

redarrow, Yesideez was being very helpful. I am nor relying on others "all the time" I was simply asking for help with a problem I am having, which is why I ma posting the question in a 'PHP help' forum.

 

Please if you havent got anything helpful to suggest, then refrain from answering.

I missed out the "." before jpg.

 

If you can add the . and try it again as I'm thinking there could be a permissions problem going on here but I'm not sure. Got my head buried in PHP this end.

 

Have a go at this - also showing the file name at the start of the script to see if we're picking it up properly.

<?php
echo 'Filename: '.$_FILES['file']['name'].'<br>';
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";

    $targetFolder='members/uploads/';
    $uploadfile=$targetFolder.'tmp'.time().'.jpg';
    //OUR DEBUG INFORMATION
    echo 'UPLOAD TO:'.$uploadFile;
    //END DEBUG INFORMATION

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
      //worked OK
      echo '<img src="'.$uploadedfile.'"/>';

      if (file_exists("members/uploads/" . $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
    } else {
      echo 'File not copied.';
    }
  }
} else {
  echo "Invalid file";
}
?>

Hi,

 

I had to change $uploadedfile and $uploadFile to $uploadfile for all the paths to be correct. Is that the right thing to have done?

 

This is what is dispayed when the script is run:

 

Filename: 47.gif

Upload: 47.gif

UPLOAD TO:members/uploads/tmp1239281489.jpg47.gif already exists.

 

And when I look in the folder, a .gif file with the above filename is displayed, but still the red 'X' :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.