acctman Posted March 1, 2008 Share Posted March 1, 2008 I'm looking for a code snipet that will upload and image and dispaly the image below on the same page after upload is complete. Instant preview of image after upload on same page. Quote Link to comment https://forums.phpfreaks.com/topic/93794-upload-and-display-image/ Share on other sites More sharing options...
ignace Posted March 1, 2008 Share Posted March 1, 2008 normally you would be able to find this on the phpfreaks main page, but since it is offline for maintenance reasons.. <?php // http://be2.php.net/manual/en/features.file-upload.php#features.file-upload.post-method $post_name = 'userfile'; $where_to = ''; // path to upload directory (no trailing slash) if (strcmp(strtolower($_SERVER['REQUEST_METHOD']), 'post') === 0) { if (array_key_exists($post_name, $_FILES)) { $upload = $_FILES[$post_name]; $where_to = implode(DIRECTORY_SEPARATOR, array($where_to, $upload['name'])); if (is_uploaded_file($upload['tmp_name'])) { if (move_uploaded_file($upload['tmp_name'], $where_to)) { printf("<img src=\"%s\" width=\"100\" height=\"100\" />", $where_to); } else { // error } } else { // nothing uploaded } } else { // $post_name not present in $_FILES } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/93794-upload-and-display-image/#findComment-480795 Share on other sites More sharing options...
acctman Posted March 3, 2008 Author Share Posted March 3, 2008 is there a way to combine the above example code with the coding below and make everything work together? pretty much i want everything to work so that uploading and displaying of the image is all done on the same page <form name="form1" method="post" action="" enctype="multipart/form-data"> <input size=50 type=file name=img><br> <input name="Submit" type="submit" value="Upload"> <?php // if(isset( $Upload )) if($_POST["action"] == "Upload") { // ============== // Configuration // ============== $uploaddir = "temp"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777! $allowed_ext = "jpg, jpeg, gif, png, bmp"; // These are the allowed extensions of the files that are uploaded $max_size = "2048000"; // 2 megabyte $max_height = "480"; // This is in pixels - Leave this field empty if you don't want to upload images $max_width = "640"; // This is in pixels - Leave this field empty if you don't want to upload images // Check Entension $extension = pathinfo($_FILES['file']['name']); $extension = $extension[extension]; $allowed_paths = explode(", ", $allowed_ext); for($i = 0; $i < count($allowed_paths); $i++) { if ($allowed_paths[$i] == "$extension") { $ok = "1"; } } // Check File Size if ($ok == "1") { if($_FILES['file']['size'] > $max_size) { print "File size is too big!"; exit; } // Check Height & Width // need to resize file if ($max_width && $max_height) { list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']); if($width > $max_width || $height > $max_height) { print "File height and/or width are too big!"; exit; } } // The Upload Part if(is_uploaded_file($_FILES['file']['tmp_name'])) { move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); } print "Your file has been uploaded successfully! Yay!"; } else { print "Incorrect file extension!"; } ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/93794-upload-and-display-image/#findComment-482128 Share on other sites More sharing options...
acctman Posted March 4, 2008 Author Share Posted March 4, 2008 can someone help me combine the two, so they work properly Quote Link to comment https://forums.phpfreaks.com/topic/93794-upload-and-display-image/#findComment-482512 Share on other sites More sharing options...
unsider Posted March 4, 2008 Share Posted March 4, 2008 action="<?php echo $_SERVER['PHP_SELF']; ?>" Something else might not work, but make the form reload the page. If this doesn't work, look in the php code for other possible solutions. Quote Link to comment https://forums.phpfreaks.com/topic/93794-upload-and-display-image/#findComment-482523 Share on other sites More sharing options...
acctman Posted March 4, 2008 Author Share Posted March 4, 2008 action="<?php echo $_SERVER['PHP_SELF']; ?>" Something else might not work, but make the form reload the page. If this doesn't work, look in the php code for other possible solutions. how much would you charge to code this correctly for me? combine my code with ignace's code for displaying image after upload. I've been at this for a few days i'd like to move on to something else. Quote Link to comment https://forums.phpfreaks.com/topic/93794-upload-and-display-image/#findComment-482997 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.