stuffradio Posted June 23, 2009 Share Posted June 23, 2009 I'm trying this Image uploader: <?php $target_path = $_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/"; $topx = $_POST['topxstart']; $topy = $_POST['topystart']; $target_path = $target_path . basename( $_FILES['file']['name']); $target_pathoriginal = $_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . basename( $_FILES['file']['name'] . 'original'); 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 />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; echo "Image: <img src=imagedisplay.php?text=" . $_POST[titletext] ."&file=".$_FILES["file"]["name"]. "&x=" . $topx . "&y=" . $topy ." border=0 />"; if (file_exists($_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } elseif (file_exists($_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . $_FILES["file"]["name"]. 'original')) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], $target_path); move_uploaded_file($_FILES["file"]["tmp_name"], $target_pathoriginal); echo "Stored in: " . file_exists($_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . $_FILES["file"]["name"]); } } } else { echo "Invalid file"; } ?> Some JPEG images I upload make the code output Invalid file. I'm not sure why it's invalid since the type of image is JPEG, 37 KB, 400 x 491 dimention. Any ideas why it would be invalid? Also, do you know of any class that would help with writing titles and captions on images and save the image to a path? Link to comment https://forums.phpfreaks.com/topic/163392-invalid-file/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 23, 2009 Share Posted June 23, 2009 That code, which you may in fact have gotten from a trusted source, is crap. It tests for upload errors after it has already attempted to test the ['type'] and ['size']. The type/size won't exist for many of the possible upload errors. It also lumps together the test for type/size, so you will never know which one of these things was the problem, and it does not tell you in the error messages why it failed to work or what the actual values were that caused it to fail. For debugging to find out what you are actually getting, add the following code - echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; And the code needs to be rewritten to test for upload errors first, then test the type, then test the size (all separately) and output a meaningful user message for upload errors, incorrect types, and invalid size (output the upload error, type and size of the uploaded file in the error messages so you will know what was wrong.) Link to comment https://forums.phpfreaks.com/topic/163392-invalid-file/#findComment-862099 Share on other sites More sharing options...
stuffradio Posted June 23, 2009 Author Share Posted June 23, 2009 I think I copied it from W3schools and edited it a bit to do what I need, where is the best and cleanest uploader do you think? Also, is it possible to do the image(insert file format here) function on the same page? Right now I need to use two php files, one for the image upload and one to display the image and write the text. Link to comment https://forums.phpfreaks.com/topic/163392-invalid-file/#findComment-862105 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.