sasori Posted September 15, 2008 Share Posted September 15, 2008 I created an formupload.php and upload.php script #### formupload.php ### <html> <body> <b>UPLOAD YOUR SHIT IN THERE</b> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="500000" /> <input type="file" name="pix" size="60" /> <input type="submit" name="upload" value="upload picture" /> </form> </body> </html> ##### upload.php #### <?php if(!isset($_POST['upload'])) { include("formupload.php"); } else { if($_FILES['pix']['tmp_name'] == "none") { echo " file not uploaded"; include("formupload.php"); exit(); } if(!preg_match("/image/",$_FILES['pix']['type'])) { echo " it's not a picture "; include("formupload.php"); exit(); } else { $destination = 'c:\wamp\tmp'."\\".$_FILES['pix']['name']; $tmp_file = $_FILES['pix']['tmp_name']; move_uploaded_file($tmp_file,$destination); echo "successfully uploaded {$_FILES['pix']['name']} ({$_FILES['pix']['size']})"; echo "<form action='upload.php' method='get'>"; echo "<input type='submit' value='Upload another file'>"; echo "</form>"; echo "<br/>"; } } ?> and the question is how am I gonna display the uploaded photo on the same page? Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/ Share on other sites More sharing options...
peranha Posted September 15, 2008 Share Posted September 15, 2008 <img src='path_to_pic.jpg' alt='' /> Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641615 Share on other sites More sharing options...
sasori Posted September 15, 2008 Author Share Posted September 15, 2008 <img src='path_to_pic.jpg' alt='' /> how ? let's say another user uploaded a photo and i don't know what the name of the photo was, all i know is the path which is c:\wamp\tmp , so how am i gonna display the photo after upload when i don't even know the name of the file ? (this bothers me) Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641617 Share on other sites More sharing options...
peranha Posted September 15, 2008 Share Posted September 15, 2008 dont you save the name of the file in a database or something? If not, I would, and if it is only displayed once, you can use this to display it once $_FILES['pix']['name'] Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641619 Share on other sites More sharing options...
sasori Posted September 15, 2008 Author Share Posted September 15, 2008 dont you save the name of the file in a database or something? If not, I would, and if it is only displayed once, you can use this to display it once $_FILES['pix']['name'] sir, i don't use a database as for the moment.. i don't know how to display the photo here' i tried this echo "<img src='".$_FILES['pix']['name']."' />"; and still it doesn't display ..can you tell me how the proper way to echo the absolute path? the images are being sent to c:\wamp\tmp folder Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641621 Share on other sites More sharing options...
peranha Posted September 15, 2008 Share Posted September 15, 2008 <?php $path = 'c:\wamp\tmp'."\\".$_FILES['pix']['name']; $extension = substr($path, -3); if($extension == "jpg" || $extension == "jpeg"){ header("Content-type: image/jpeg"); }elseif($extension == "gif"){ header("Content-type: image/gif"); }elseif($extension == "bmp"){ header("Content-type: image/bmp"); } readfile($path); ?> echo "<img src='$path' />"; try that and see what you get. Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641626 Share on other sites More sharing options...
sasori Posted September 15, 2008 Author Share Posted September 15, 2008 i edited my script and inserted your logic code <?php if(!isset($_POST['upload'])) { include("formupload.php"); } else { if($_FILES['pix']['tmp_name'] == "none") { echo " file not uploaded"; include("formupload.php"); exit(); } if(!preg_match("/image/",$_FILES['pix']['type'])) { echo "<b>LOL! it's not a picture</b>"; include("formupload.php"); exit(); } else { $destination = 'c:\wamp\tmp'."\\".$_FILES['pix']['name']; $tmp_file = $_FILES['pix']['tmp_name']; move_uploaded_file($tmp_file,$destination); echo "<b>successfully uploaded {$_FILES['pix']['name']} ({$_FILES['pix']['size']})<b>"; echo "<form action='upload.php' method='get'>"; echo "<input type='submit' value='Upload another file'>"; echo "</form>"; echo "<br/>"; echo "<br/>"; $path = 'c:\wamp\tmp'."\\".$_FILES['pix']['name']; $extension = substr($path, -3); if($extension == "jpg" || $extension == "jpeg") { header("Content-type: image/jpeg"); } elseif($extension == "gif") { header("Content-type: image/gif"); } elseif($extension == "bmp") { header("Content-type: image/bmp"); } elseif($extension == "png") { header("Content-type: image/png"); } readfile($path); echo "<img src='$path'/>"; } } ?> it got worst sir ??? Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641629 Share on other sites More sharing options...
peranha Posted September 15, 2008 Share Posted September 15, 2008 Put this in a different file call it getimage.php <?php $path = $_GET['id']; $extension = substr($path, -3); if($extension == "jpg" || $extension == "jpeg"){ header("Content-type: image/jpeg"); }elseif($extension == "gif"){ header("Content-type: image/gif"); }elseif($extension == "bmp"){ header("Content-type: image/bmp"); } readfile($path); ?> then on your page call it like this echo "<img src='getimage.php?id=" . $destination . "' alt='' />"; This worked on my server. Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641636 Share on other sites More sharing options...
sasori Posted September 15, 2008 Author Share Posted September 15, 2008 any other way sir? i did what you said $destination = 'c:\wamp\tmp'."\\".$_FILES['pix']['name']; $tmp_file = $_FILES['pix']['tmp_name']; move_uploaded_file($tmp_file,$destination); echo "<b>successfully uploaded {$_FILES['pix']['name']} ({$_FILES['pix']['size']})<b>"; echo "<form action='upload.php' method='get'>"; echo "<input type='submit' value='Upload another file'>"; echo "</form>"; echo "<br/>"; echo "<br/>"; echo "<img src='getimage.php?id". $destination."' alt=''/>"; still nothing happened..it just uploads the file and no display at all Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641642 Share on other sites More sharing options...
DarkWater Posted September 15, 2008 Share Posted September 15, 2008 You forgot the '=' after id in the URL Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641643 Share on other sites More sharing options...
sasori Posted September 15, 2008 Author Share Posted September 15, 2008 You forgot the '=' after id in the URL thank you to both of you..it is working fine now cheers Quote Link to comment https://forums.phpfreaks.com/topic/124250-solved-display-uploaded-file/#findComment-641646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.