beemer832 Posted October 27, 2010 Share Posted October 27, 2010 I am working on a PHP project that will allow a user to upload a handful of images, and then submit text to go along with it. To simplify the uploading process of a file, I restricted the type of file that can be uploaded (jpg) and the size (2.5mb). The problem is, the form is calling to a PHP script, and I have error logging built in to alert the user of why a file didn't upload, but since the PHP script is just being called, no errors are being displayed on the page. I guess unless I do a refresh of a few seconds to view any errors on the page.... What I would like to do is show a pop-up/error message notifying the user that the file type or size is incorrect and needs to be adjusted. Now part of the problem is, the file needs to be uploaded to the server first, and then everything can be checked, so I am really getting confused. If someone can point me in the right direction on what I could use to accomplish this, it would be greatly appreciated. Thanks -beemer Link to comment https://forums.phpfreaks.com/topic/216949-htmlphpjavascript-help-image-size-and-extension-form-validation/ Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2010 Share Posted October 27, 2010 Can you post the current code you're using for handling/validating the form? Link to comment https://forums.phpfreaks.com/topic/216949-htmlphpjavascript-help-image-size-and-extension-form-validation/#findComment-1126923 Share on other sites More sharing options...
beemer832 Posted October 28, 2010 Author Share Posted October 28, 2010 This is the code that uploading the file, checking for size and file type, and then displaying errors on the page. Thanks for moving the thread, didn't know where it needed to be posted. <? header("location: /classifieds/index.php"); echo '<html><center>'; //first lets upload any files that were selected// $date = date("m/d/y",time()); //check that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 2500000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/uploads/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { //strip off file path for $newname variable so path is not accessible via html// $tempnewname = explode('/', $newname); echo $tempnewname; $newname=$tempnewname[9].'/'.$tempnewname[10]; echo "It's done! The file has been saved as: ".$filename; } else { echo "Error: A problem occurred during file upload!"; } } else { //echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";// $timestampname = str_replace('.jpg', date('j-n-Y_g:i:s').'.jpg', (basename($_FILES['uploaded_file']['name']))); $path = dirname(__FILE__).'/uploads/'; $fullname = $path.$timestampname; //strip off file path for $newname variable so path is not accessible via html// $tempnewname = explode('/', $fullname); $newname=$tempnewname[7].'/'.$tempnewname[8]; $picname=$tempnewname[8]; ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))); } } else { echo "Error: Only .jpg images under 2.5MB are accepted for upload"; } } else { echo "Error: No file uploaded"; } ?> Link to comment https://forums.phpfreaks.com/topic/216949-htmlphpjavascript-help-image-size-and-extension-form-validation/#findComment-1127741 Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 Why is the first thing in that script a header() redirect? Is there more code that you haven't posted? Link to comment https://forums.phpfreaks.com/topic/216949-htmlphpjavascript-help-image-size-and-extension-form-validation/#findComment-1127752 Share on other sites More sharing options...
beemer832 Posted November 1, 2010 Author Share Posted November 1, 2010 Great question.... I have included the entire PHP page below, I was leaving out the DB information and some image manipulation code. I am redirecting the page back to the main page, index.php as there is no data on this page. Is this incorrect? <? header("location: /classifieds/index.php"); echo '<html><center>'; //first lets upload any files that were selected// $date = date("m/d/y",time()); //check that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 2500000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/uploads/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { //strip off file path for $newname variable so path is not accessible via html// $tempnewname = explode('/', $newname); echo $tempnewname; $newname=$tempnewname[9].'/'.$tempnewname[10]; echo "It's done! The file has been saved as: ".$filename; } else { echo "Error: A problem occurred during file upload!"; } } else { //echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";// $timestampname = str_replace('.jpg', date('j-n-Y_g:i:s').'.jpg', (basename($_FILES['uploaded_file']['name']))); $path = dirname(__FILE__).'/uploads/'; $fullname = $path.$timestampname; //strip off file path for $newname variable so path is not accessible via html// $tempnewname = explode('/', $fullname); $newname=$tempnewname[7].'/'.$tempnewname[8]; $picname=$tempnewname[8]; ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))); } } else { echo "Error: Only .jpg images under 2.5MB are accepted for upload"; } } else { echo "Error: No file uploaded"; } ?> <? //Convert full size image to 1-thumbnail size 2-similar size for all images. files will be saved in corresponding folders// include('SimpleImage.php'); $image = new SimpleImage(); $image->load($fullname); $image->resize(175, 115); $image->save("images/thumb/2$picname"); $thumbpic="images/thumb/2$picname"; $image = new SimpleImage(); $image->load($fullname); $image->resize(640, 480); $image->save("images/full/2$picname"); $fullpic="images/full/2$picname"; ?> <? //now lets enter this information into the DB// include'dbconnect.php'; $title=$_POST['title']; $description=$_POST['description']; //$images=$_POST['uploaded_file'];// $price=$_POST['price']; mysql_connect($hostname,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO parts VALUES ('','$title','$description','$thumbpic','$fullpic','$price')"; mysql_query($query); mysql_close(); ?> </html> thanks -Beemer Link to comment https://forums.phpfreaks.com/topic/216949-htmlphpjavascript-help-image-size-and-extension-form-validation/#findComment-1128825 Share on other sites More sharing options...
Pikachu2000 Posted November 1, 2010 Share Posted November 1, 2010 Yes, that would be incorrect. As soon as the script loads, it redirects to index.php. It may or may not execute some or none of the code in it. That script needs to execute to process the upload and insert the information in the database. Link to comment https://forums.phpfreaks.com/topic/216949-htmlphpjavascript-help-image-size-and-extension-form-validation/#findComment-1128839 Share on other sites More sharing options...
beemer832 Posted November 1, 2010 Author Share Posted November 1, 2010 okay good to know for future notice, I will update that. the code is working now though. the file is uploaded. thanks -beemer Link to comment https://forums.phpfreaks.com/topic/216949-htmlphpjavascript-help-image-size-and-extension-form-validation/#findComment-1129019 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.