Swarfega Posted December 28, 2012 Share Posted December 28, 2012 Hi. I've got an issue with a simple File (Image) Upload Script, it's the following: $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["profilepic"]["name"])); if ((($_FILES["profilepic"]["type"] == "image/gif") || ($_FILES["profilepic"]["type"] == "image/jpeg") || ($_FILES["profilepic"]["type"] == "image/png") || ($_FILES["profilepic"]["type"] == "image/pjpeg")) && ($_FILES["profilepic"]["size"] < 2000000000) && in_array($extension, $allowedExts)) { if ($_FILES["profilepic"]["error"] > 0) { echo "Return Code: " . $_FILES["profilepic"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["profilepic"]["name"] . "<br>"; echo "Type: " . $_FILES["profilepic"]["type"] . "<br>"; echo "Size: " . ($_FILES["profilepic"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["profilepic"]["tmp_name"] . "<br>"; $dir = "images/profilepics/". $_SESSION['SESS_MEMBER_ID']; if(!is_dir($dir)){ mkdir($dir); } move_uploaded_file($_FILES["profilepic"]["tmp_name"], $dir . "/profile.png"); $newpicpath = $dir . "/profile.png"; } } else { echo "Invalid file"; } Now the actual issue at hand is the following: I've got 1 Live-Test Script and one not-Live Script to test this on, and the problem is, the script itself (Creating the Dir, Moving the Image) only works on the Non-Live Script (Test2.php). However, using the EXACT same code snippet from the Non-Live Script(Test2.php) in the Live Script (ModifyProfile.php), it returns with "Invalid file", regardless of using the same image on both the PHP's. Anyone got a clue as to why this is? Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/ Share on other sites More sharing options...
cpd Posted December 28, 2012 Share Posted December 28, 2012 (edited) Confirm your input tag names are correct in your live HTML. If that fails debug each condition of your first if statement individually to see which condition it fails on. Edited December 28, 2012 by CPD Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401890 Share on other sites More sharing options...
Swarfega Posted December 28, 2012 Author Share Posted December 28, 2012 echo '<strong>Profil-Bild:</strong> <input type="file" name="profilepic"> '; This is the Input line, I've overlooked the other ~10+ input tags and they're all ok as well, I also tried debugging but it always goes back to 'Invalid File'. Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401898 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2012 Share Posted December 28, 2012 If the same code works on a different server, your form is likely okay. Unfortunately, the upload code you are using is based on the w3schools example and it is crap. It tests for upload errors after it has tried to use some of the uploaded file information, so if there is an upload error, that code will indicate an invalid file and will never actually display the upload error code. You must test if the upload worked before you can use any of the , [type], [tmp_name], or [name] values. There's a whole host of other problems with that code, such as not lumping together the type and size validation tests, because you will never know which condition caused the validation to fail (wouldn't it be a good idea to tell the visitor if the file was the wrong type OR that he uploaded a file that was too big, so that he could correct what he did), and when validating user supplied input, you should display the incorrect value that failed a test, along with the accepted values. For debugging purposes, add the following code right before the code that you posted - echo '<pre>',print_r($_FILES,true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401899 Share on other sites More sharing options...
Swarfega Posted December 28, 2012 Author Share Posted December 28, 2012 After adding your debug line, this is what I get Array ( ) Invalid file Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401901 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2012 Share Posted December 28, 2012 Either uploads are not enabled on the server or the size of the file you are trying to upload is larger then the post_max_size setting, or you actually have something different about your form, such as nesting it inside of another form or the <form tag doesn't have the enctype attribute. What does a phpinfo statement show for the file_uploads and post_max_size settings and what size of file did you try to upload? Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401903 Share on other sites More sharing options...
Swarfega Posted December 28, 2012 Author Share Posted December 28, 2012 Form tag: echo '<form action="andra.php?uid='.$_SESSION['SESS_MEMBER_ID'].'&send=true" method="post">'; PHPInfo(): [b]file_uploads:[/b] On [b]post_max_size:[/b] 128M Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401906 Share on other sites More sharing options...
Swarfega Posted December 28, 2012 Author Share Posted December 28, 2012 I added the enctype tag and it's working properly now, thanks a lot. I honestly didn't even know what that was! Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401907 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2012 Share Posted December 28, 2012 That <form tag cannot upload a file. It's missing the enctype="multipart/form-data" attribute. Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401908 Share on other sites More sharing options...
Swarfega Posted December 28, 2012 Author Share Posted December 28, 2012 On a different note, is it possible to add a Function snippet into the code I posted in order to resize the Image without screwing up the pixels on a 'large' scale? Currently I'm just limiting the picture within the border using height=x and width=x in the <img src> tag, which compresses the pixels quite a lot Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401910 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2012 Share Posted December 28, 2012 Php has a GD image extension that is typically used to resize images. If you search for PHP GD resize script, I'm sure you can find something to try/test with. Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401911 Share on other sites More sharing options...
Swarfega Posted December 28, 2012 Author Share Posted December 28, 2012 Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/272458-file-upload-messing-up/#findComment-1401913 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.