October 6, 200917 yr if(empty($prod_name)) { $errors[] = 'Please insert a product name.'; } if(empty($cat_name) && ($cat_desc)){ $errors[] = 'Please enter the catagory name and description!'; } if(empty($prod_price)) { $errors[] = 'Please enter the products price.'; } $allowed = array ('image/jpg','image/jpeg','image/JPG','image/JPEG','image/X-PNG','image/PNG','image/png','image/x-png'); if (in_array($_FILES['upload']['type'], $allowed)){ if (is_uploaded_file($_FILES['upload']['tmp_name'])) { //create temporary filename $temp = '../../upload/'. $_FILES['upload']['name']; //move file over if (move_uploaded_file($_FILES['upload']['tmp_name'],$temp)){ $thankyou[] = 'The file has been uploaded'; //set image name $temp = NULL; $i = $_FILES['upload']['name']; }else{//couldnt move over $errors[] = 'The file could not be moved'; $temp = $_FILES['upload']['tmp_name']; } }else{ $errors[] = 'No file was uploaded'; $temp = NULL; } }else{ $errors[] = 'Please use JPG or PNG images.'; } //validate the catagory if (isset($_POST['category']) && ($_POST['category'] == 'new') ) { //if its a new catagory add it to database //add catagory to database. $sql = 'INSERT INTO category (id, cat_name, cat_desc) VALUES (NULL, "'.$cat_name.'", "'.$cat_desc.'")'; $r = mysqli_query($dbc,$sql); if ($r) {// if it ran ok $thankyou[] ='Catagory added to database.'; }else{ $errors[] = 'Catagory could not be added to database.'; echo 'Error' . mysqli_error($dbc) . 'Query' . $q .'.'; } mysqli_close($dbc); $errors[] = 'Enter the catagory details.'; }elseif(isset($_POST['category']) && ($_POST['category'] == 'existing') && ($_POST['existing'] > 0)){//existing catagory $a = (int) $_POST['existing']; }else{ $errors[] = 'Please select the products catagory!'; } if(empty($errors)) {//everything is okay // add the prodcut to the database. $q = 'INSERT INTO product(prod_id, prod_name, prod_desc, prod_price, prod_imagename, prod_dim) VALUES (NULL,"'.$prod_name.'","'.$prod_desc.'","'.$prod_price.'","'.$i.'","'.$prod_dim.'")'; $r = mysqli_query($dbc, $q); //check results if (mysqli_affected_rows($dbc) == 1){ //true $thankyou[] = 'The print has been added'; //clear $_POST $_POST = array(); }else{ //error $errors[] = 'There has been a system error and your submission could not be processed'; } mysqli_close($dbc); }//end of errors if //delete the upload file if it still exists if (isset($temp) && file_exists ($temp) && is_file($temp)){ unlink($temp); } $_POST = array(); }//end of page submit This is an upload script i have produced, trouble is, at times it decides to upload, then other times it does, as you can see i have made an array to limit it to png or jpg. I can't see why it won't upload. I still have to get it to get the image and resize it incase the pixel dimension is just too big for screen displays. Anyone shed any light onto what might be causing this? Also i'm having trouble once this has inputed if the picture successfully uploads. to get the product id from the database and rename the image to the product id so it matches. Many Thanks Ben
October 7, 200917 yr if(empty($prod_name)) { $errors[] = 'Please insert a product name.'; } if(empty($cat_name) && ($cat_desc)){ $errors[] = 'Please enter the catagory name and description!'; } if(empty($prod_price)) { $errors[] = 'Please enter the products price.'; } $allowed = array ('image/jpg','image/jpeg','image/JPG','image/JPEG','image/X-PNG','image/PNG','image/png','image/x-png'); if (in_array($_FILES['upload']['type'], $allowed)){ if (is_uploaded_file($_FILES['upload']['tmp_name'])) { //create temporary filename $temp = '../../upload/'. $_FILES['upload']['name']; //move file over if (move_uploaded_file($_FILES['upload']['tmp_name'],$temp)){ $thankyou[] = 'The file has been uploaded'; //set image name $temp = NULL; $i = $_FILES['upload']['name']; }else{//couldnt move over $errors[] = 'The file could not be moved'; $temp = $_FILES['upload']['tmp_name']; } }else{ $errors[] = 'No file was uploaded'; $temp = NULL; } }else{ $errors[] = 'Please use JPG or PNG images.'; } //validate the catagory if (isset($_POST['category']) && ($_POST['category'] == 'new') ) { //if its a new catagory add it to database //add catagory to database. $sql = 'INSERT INTO category (id, cat_name, cat_desc) VALUES (NULL, "'.$cat_name.'", "'.$cat_desc.'")'; $r = mysqli_query($dbc,$sql); if ($r) {// if it ran ok $thankyou[] ='Catagory added to database.'; }else{ $errors[] = 'Catagory could not be added to database.'; echo 'Error' . mysqli_error($dbc) . 'Query' . $q .'.'; } mysqli_close($dbc); $errors[] = 'Enter the catagory details.'; }elseif(isset($_POST['category']) && ($_POST['category'] == 'existing') && ($_POST['existing'] > 0)){//existing catagory $a = (int) $_POST['existing']; }else{ $errors[] = 'Please select the products catagory!'; } if(empty($errors)) {//everything is okay // add the prodcut to the database. $q = 'INSERT INTO product(prod_id, prod_name, prod_desc, prod_price, prod_imagename, prod_dim) VALUES (NULL,"'.$prod_name.'","'.$prod_desc.'","'.$prod_price.'","'.$i.'","'.$prod_dim.'")'; $r = mysqli_query($dbc, $q); //check results if (mysqli_affected_rows($dbc) == 1){ //true $thankyou[] = 'The print has been added'; //clear $_POST $_POST = array(); }else{ //error $errors[] = 'There has been a system error and your submission could not be processed'; } mysqli_close($dbc); }//end of errors if //delete the upload file if it still exists if (isset($temp) && file_exists ($temp) && is_file($temp)){ unlink($temp); } $_POST = array(); }//end of page submit This is an upload script i have produced, trouble is, at times it decides to upload, then other times it does, as you can see i have made an array to limit it to png or jpg. I can't see why it won't upload. I still have to get it to get the image and resize it incase the pixel dimension is just too big for screen displays. Anyone shed any light onto what might be causing this? Also i'm having trouble once this has inputed if the picture successfully uploads. to get the product id from the database and rename the image to the product id so it matches. Many Thanks Ben Hi Ben, Maybe its just me being tired or summot but not all of that made sense to me, can you please break it down into sections what it is your looking to do. Kyle
October 7, 200917 yr Hey Ben, What happens when it doesn't upload? Can you try uploading a small file and a large file then let me know the outcomes? It could be that you're hitting your PHP file upload size limit. If this is the case then there are workarounds, but lets make sure first. Cheers, Aaron.
October 7, 200917 yr At the top of your script add; error_reporting(-1); It could be anything really (if you aren't receiving any syntax errors). In regards to file uploads a common problem is permissions. To find out where it's failing try add die statements to the end of functions to narrow the problem down.
October 7, 200917 yr Author I think it is the file size, as I've managed to upload files that are so to speak no big in file size, but ones that would be big in file size dont upload, how do i find out my upload limit, would it be in my phpinfo file? and the only error i am receiving is should upload a jpg or png, I'm guessing, if i find out my upload limit, i can add a condition into the script to say whether the file size is too big? Thanks for all the help so far. P.s not all the script is in the first post. all the script before hand works as its only collection of variables. Ben
October 8, 200917 yr Yeah look for the upload_max_filesize parameter in php_info. There are a two easy ways you can change it: 1 - Alter your PHP.ini file if you have access to it. 2 - In your .htaccess file, add: php_value upload_max_filesize 100M Cheers, Aaron.
October 8, 200917 yr Author Yeah look for the upload_max_filesize parameter in php_info. There are a two easy ways you can change it: 1 - Alter your PHP.ini file if you have access to it. 2 - In your .htaccess file, add: php_value upload_max_filesize 100M Cheers, Aaron. Thank you for the reply, I just checked out my info page and it says 6m which i assume is 6MB? But the images i tried i uplaoding when testing this script were just over 3MB but not above 4mb. Is there anything else that might cause this problem? I'm going to try and change the value to 10mb see what that does. Thanks for the help so far. Ben
October 8, 200917 yr Yeah M refers to MB. Hm I guess you could be exceeding the PHP execution time.I wouldn't have thought it will be permissions if some files upload and some don't. What happens for an upload that fails? Check php_info for 'max_execution_time'. The value is measured in seconds. Does your upload take longer that what is specified?
October 8, 200917 yr Author Yeah M refers to MB. Hm I guess you could be exceeding the PHP execution time.I wouldn't have thought it will be permissions if some files upload and some don't. What happens for an upload that fails? Check php_info for 'max_execution_time'. The value is measured in seconds. Does your upload take longer that what is specified? My execution time is 20, so 20 secs if im using general knowledge correctly. I'll pm you the link to the script so you can try it out yourself with different files sizes. I haven't changed the upload limit yet as i'm currently away from my main pc. Thanks for the help so far, you'll have a pm waiting. Ben
October 8, 200917 yr OK I was able to upload an image that was about 70kb without any problems. It took only a few seconds to do so. When I tried to upload an image that was about 2.2MB it took much longer than 20 seconds and failed. I would suggest taking a look at http://us.php.net/manual/en/function.set-time-limit.php in order to extend the PHP execution time limit.
October 8, 200917 yr Author OK I was able to upload an image that was about 70kb without any problems. It took only a few seconds to do so. When I tried to upload an image that was about 2.2MB it took much longer than 20 seconds and failed. I would suggest taking a look at http://us.php.net/manual/en/function.set-time-limit.php in order to extend the PHP execution time limit. Thank you, it looks like a time issue then, This will be used on a different server in the end so ill book mark this thread so i can recall back to it for reference when the site gets put onto its real server. Thank you for the help Ben
Create an account or sign in to comment