July 24, 20179 yr I'm submitting a form where a user can add an image file... <form method="post" action=".\sendContact.php" enctype="multipart/form-data"> <input type="TEXT" name="email"> <input type="file" name="file"> <input type="submit" value="Submit"> to a php ... echo 'file test '; echo $_POST['email']; echo ' '; echo $_FILES['file']['tmp_name']; echo ' '; if ($_FILES['file']['error'] == 0) { echo 'file ok'; } echo ' '; and get file test 3@3.com C:\Windows\Temp\php99DD.tmp file ok but if I include if(getimagesize($_FILES['file']['tmp_name'])){ echo 'image ok'; } I get 500- Internal server error php.ini file_uploads = On upload_max_filesize = 2M max_file_uploads = 20 any ideas? this is my 3rd post but so far no response, can someone confirm that I'm not doing something wrong and that the site is still active? Edited July 24, 20179 yr by fnets
July 24, 20179 yr Hi Matey, I think it's because you are trying to get the size of an error and getimagesize() only works on images. The following will check it exists... if(file_exists($_FILES['file']['tmp_name'])) { echo "I Exist"; } and so if it exists we can work out how big it is ... if(file_exists($_FILES['file']['tmp_name'])) { if(filesize($_FILES['file']['tmp_name'])>2097152) { // Uses Bytes echo "<b>Error!</b> - The file is bigger than 2MB."; } } Edited July 24, 20179 yr by BrowserBugs
July 24, 20179 yr Author thank you. if(file_exists($_FILES['file']['tmp_name'])) {echo "I Exist";} didn't echo so it doesn't exist? does that mean file_exists($_FILES['file']['tmp_name']) is incorrect. if so what should I do??
July 24, 20179 yr Try ... if(!empty($_FILES['file']['name'])) { echo "There is a file"; list($source_width, $source_height, $source_type) = getimagesize($_FILES['file']['tmp_name']); echo $source_width . " x " . $source_height . " type " . $source_type; } else { echo "No File"; } This should let you know about the image. Thing is you'd be better to look into file info if it's enabled, much safer. Edit; e.g. $fileinfo = finfo_open(FILEINFO_MIME_TYPE); $temp_type = finfo_file($fileinfo, $_FILES['file']['tmp_name']); finfo_close($fileinfo); if($temp_type=="image/png") { $ext = "png"; } elseif($temp_type=="image/jpeg") { $ext = "jpg"; } else { echo "<b>Error!</b> - The file is not a png or jpg."; } Edited July 24, 20179 yr by BrowserBugs
July 25, 20179 yr Author if(!empty($_FILES['file']['name'])) { echo 'There is a file'; } if(!empty($_FILES['file']['tmp_name'])) { echo 'There is a file tmp_name'; } neither of the above echo's get called. Does that mean perhaps the file is deleted by the server before my code gets to it? BTW. thanks for your help, very much appreciated Edited July 25, 20179 yr by fnets
July 25, 20179 yr Author echo '<pre>'; print_r($_FILES);echo '</pre>'; prints Array ( [file] => Array ( [name] => sn.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php9BED.tmp [error] => 0 => 34186 ) ) ; Edited July 25, 20179 yr by fnets
July 25, 20179 yr if(!empty($_FILES['file']['name'])) { echo 'There is a file'; } if(!empty($_FILES['file']['tmp_name'])) { echo 'There is a file tmp_name'; } neither of the above echo's get called. Does that mean perhaps the file is deleted by the server before my code gets to it? BTW. thanks for your help, very much appreciated The temp file will be deleted after the full php execution, if(!empty()) { should fire if there was a file uploaded. Bit baffled mate as your print_r($_FILES); says there was a file. What happen with the following? <?php if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_FILES['image_up']['name']) && intval($_POST['uploadFile'])==1) { echo "I Got a file"; } ?> <form method="post" enctype="multipart/form-data"> <input name="image_up" type="file"> <button type="submit" name="uploadFile" value="1">Upload</button> </form> This in a blank page should work fine. On a side note i'm always cautious of using reserved words for field names.
July 25, 20179 yr Author have to go out this pm but tried briefly and got errors. I think I need to go back to something very simple, going to try this http://form.guide/contact-form/contact-form-attachment.html
July 26, 20179 yr Author The temp file will be deleted after the full php execution, if(!empty()) { should fire if there was a file uploaded. Bit baffled mate as your print_r($_FILES); says there was a file. What happen with the following? <?php if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_FILES['image_up']['name']) && intval($_POST['uploadFile'])==1) { echo "I Got a file"; } ?> <form method="post" enctype="multipart/form-data"> <input name="image_up" type="file"> <button type="submit" name="uploadFile" value="1">Upload</button> </form> This in a blank page should work fine. On a side note i'm always cautious of using reserved words for field names. ran on new page and got 405 - HTTP verb used to access this page [.html] is not allowed. any ideas? Edited July 26, 20179 yr by fnets
July 26, 20179 yr Just checking the page you made was a .php and not a .html? Is this an IIS server? Digging up that error code it says you require an action so ... <?php if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_FILES['image_up']['name']) && intval($_POST['uploadFile'])==1) { echo "I Got a file"; } ?> <form method="post" action="/thepage.php" enctype="multipart/form-data"> <input name="image_up" type="file"> <button type="submit" name="uploadFile" value="1">Upload</button> </form>
July 26, 20179 yr Author my mistake. now a .php and it worked "I got a file". Yes IIS server. learning so much here, thank you. if I separate into .html and .php they also work. so now I have something to build on. cheers. and this works ... <?php echo '<pre>'; print_r($_FILES);echo '</pre>'; if(file_exists($_FILES['image_up']['tmp_name'])) { echo "file exists"; } ?> Edited July 26, 20179 yr by fnets
Create an account or sign in to comment