pcw Posted April 8, 2009 Share Posted April 8, 2009 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 More sharing options...
pcw Posted April 8, 2009 Author Share Posted April 8, 2009 Ok, I got this now, which point img src to the right directory: echo '<img src="members/uploads/" . $_FILES["file"]["name"]>'; but all i get is a small box with a red cross, instead of the uploaded image. Does anyone know how to fix this? Thanks Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-804881 Share on other sites More sharing options...
pcw Posted April 8, 2009 Author Share Posted April 8, 2009 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? Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-804915 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 Can anyone help me with this please? Thanks Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805388 Share on other sites More sharing options...
Yesideez Posted April 9, 2009 Share Posted April 9, 2009 $_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 } Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805392 Share on other sites More sharing options...
Yesideez Posted April 9, 2009 Share Posted April 9, 2009 Try right-clicking the red "X" in your browser and see where it thinks the image is and then check to see where the image REALLY is and that shold give you more of an insight as to where your script isn't working. Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805396 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805404 Share on other sites More sharing options...
Yesideez Posted April 9, 2009 Share Posted April 9, 2009 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805410 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 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 Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805425 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 Even typing the location of a image file is resulting in a red "X" eg. print '<img src="members/uploads/03.gif">'; What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805428 Share on other sites More sharing options...
redarrow Posted April 9, 2009 Share Posted April 9, 2009 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805429 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 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. Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805439 Share on other sites More sharing options...
redarrow Posted April 9, 2009 Share Posted April 9, 2009 $targetFolder='members/uploads/'; to $targetFolder='/members/uploads/'; try Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805444 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 Hi, thanks for your reply. When I run the script now, all what I get is Upload: 48.gif UPLOAD TO: File not copied. And an image field is not displayed at all. I cant figure out why something so simple doesnt seem to be working lol Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805450 Share on other sites More sharing options...
Yesideez Posted April 9, 2009 Share Posted April 9, 2009 Try this a sec and change this line: $uploadfile=$targetFolder.'tmp'.time().'jpg'; This should generate the filename "tmp" with a number then .jpg - try uploading a JPEG and see what happens. Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805452 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 Hi Yesideez, thanks for your reply, that still shows the red 'X' and creates a file called /public_html/cgi-bin/sitebuilder/members/uploads/tmp1239280691jpg but it just contains a lot of gobbledegook. What are we able to work out from this? Thanks again Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805458 Share on other sites More sharing options...
Yesideez Posted April 9, 2009 Share Posted April 9, 2009 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805469 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 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' Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805479 Share on other sites More sharing options...
pcw Posted April 9, 2009 Author Share Posted April 9, 2009 Thanks Yesideez, it kind of clicked when you mentioned permissions. I changed the upload path to a folder outside of the cgi-bin and it works fine now. Thanks for all your help Link to comment https://forums.phpfreaks.com/topic/153214-solved-show-uploaded-image/#findComment-805503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.