Kalam1ty Posted August 22, 2011 Share Posted August 22, 2011 I have been following a tutorial to create my first php application, a file (image) uploading system. I am hoping to add it to my site, http://www.kalam1ty.com/upload.php I have read through the code multiple times and cannot find out what is wrong. I have tried to fix this for about half an hour now and cannot seem to find where the code is wrong. It does not generate any errors and passes all of the checks that I wrote, it just does not upload the image. Nothing in the upload part of the script looks off to me, but I am a PHP newbie, and am not 100% sure. Here is the whole page's code: <?php if(isset($_FILES['image'])) { $errors = array(); $allowed_ext = array('jpg', 'jpeg', 'png', 'gif'); $file_name = $_FILES['image']['name']; $file_ext = strtolower(end(explode('.', $file_name ))); $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp']; if(in_array($file_ext, $allowed_ext) === false) { $errors[] = 'Extionsion not allowed.'; } if($file_size > 10485760) { $errors[] = 'File too large. Must be under 10MB.'; } if(empty($errors)) { //upload file if (move_uploaded_file($file_tmp, 'uploads/images'.$file_name)) { echo 'File uploaded to kalam1ty.com/uploads/images/'; } } else { foreach ($errors as $error) { echo $error, '<br>'; } } } ?> <html> <head> <title> Kalam1ty - Home </title> <link rel="stylesheet" type="text/css" href="style.css" /> <link href='http://fonts.googleapis.com/css?family=Anonymous+Pro' rel='stylesheet' type='text/css'> <link rel="shortcut icon" href="/home/corona/Development/Web/favicon.ico" type="image/x-icon" /> <meta name="google" value="notranslate"> </head> <body> <div id="EncBG"> <div id="Navbar"> <a href="index.php">Home</a> <a href="downloads.php">Downloads</a> <a href="upload.php">Upload</a> <a href="contact.php">Contact</a> </div> <div id="Enc1"> <div id="Content"> <h2>Upload Files</h2> <form action='' method='POST' enctype='multipart/form-data'> <p> <input type='file' name='image'> <input type='submit' value='Upload'> </p> </form> </div> </div> <div id="Footer" class="whitetext" align="center"> <b><i> <a href="index.html">HOME</a> // <a href="downloads.html">DOWNLOADS</a> // <a href="#">OTHER</a> </b></i><br> <img src="smlogo.png" alt="Kalam1ty"/> </div> </div> </body> </html> Thank you guys very much if you can help me! Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/ Share on other sites More sharing options...
dougjohnson Posted August 22, 2011 Share Posted August 22, 2011 Do you have rights on the server to create files at the upload location? Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260676 Share on other sites More sharing options...
Kalam1ty Posted August 22, 2011 Author Share Posted August 22, 2011 Do you have rights on the server to create files at the upload location? I'm not sure. Would I chmod upload.php or the image directory? What would I chmod to? Thanks for actually trying to help me. Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260678 Share on other sites More sharing options...
dougjohnson Posted August 22, 2011 Share Posted August 22, 2011 You need to make sure the "directory" you are uploading to has the correct permissions set. In this case, you need to be able to "write" files to your uploads directory. So, right click the directory "on the server", click properties, and set the permissions for the "website" user. Or you could use chmod. Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260682 Share on other sites More sharing options...
Kalam1ty Posted August 22, 2011 Author Share Posted August 22, 2011 I added write permission for users to the uploads and images directories. It still seems that the photos are not uploaded correctly. Could it be something with the code and not permissions? Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260684 Share on other sites More sharing options...
dougjohnson Posted August 22, 2011 Share Posted August 22, 2011 I think you need to add a file name to your <form action='' tag? Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260688 Share on other sites More sharing options...
PFMaBiSmAd Posted August 22, 2011 Share Posted August 22, 2011 You should be developing and debugging your code with error_reporting set to E_ALL (or to a -1) and display_errors set to ON so that all the php detected errors will be reported and displayed. Add the following two lines of code immediately after your first opening <?php tag - ini_set("display_errors", "1"); error_reporting(-1); You should be getting an error concerning the destination folder/file (you are missing a / after 'images' and before the filename.) Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260689 Share on other sites More sharing options...
Kalam1ty Posted August 22, 2011 Author Share Posted August 22, 2011 You should be developing and debugging your code with error_reporting set to E_ALL (or to a -1) and display_errors set to ON so that all the php detected errors will be reported and displayed. Add the following two lines of code immediately after your first opening <?php tag - ini_set("display_errors", "1"); error_reporting(-1); You should be getting an error concerning the destination folder/file (you are missing a / after 'images' and before the filename.) Thanks for your help! Now that that error is solved, I am getting another. Strict Standards: Only variables should be passed by reference in /usr/www/kalam1ty/public/upload.php on line 10 Notice: Undefined index: tmp in /usr/www/kalam1ty/public/upload.php on line 12 I'm not sure what the error means, or what undefined index means. Again, help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260692 Share on other sites More sharing options...
PFMaBiSmAd Posted August 22, 2011 Share Posted August 22, 2011 The first error is because end expects an actual array variable to reference. You need to assign the result of the explode to a variable, then use that variable in the remainder of the functions. The second error is because the field is - ['tmp_name'] Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260696 Share on other sites More sharing options...
Kalam1ty Posted August 22, 2011 Author Share Posted August 22, 2011 The first error is because end expects an actual array variable to reference. You need to assign the result of the explode to a variable, then use that variable in the remainder of the functions. The second error is because the field is - ['tmp_name'] Thank you so much! I got it working because of you. The first error can be ignored, but I'm going to try to find out how to fix it. I know you told me how, but I am not very familiar with php, so I'll need to do a little bit of research first. Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260697 Share on other sites More sharing options...
PFMaBiSmAd Posted August 22, 2011 Share Posted August 22, 2011 You might be able to ignore the first error, but php won't. Even with error_reporting/display_errors set so that error is not reported/displayed, php must still detect that error and call the error handler routine every time that line of code is executed. The reporting/display/logging of the error is just the last step in the error handler before it returns to the calling code. Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260700 Share on other sites More sharing options...
Kalam1ty Posted August 22, 2011 Author Share Posted August 22, 2011 Well, I meant that it would function properly (or so it seemed), but the error would still appear there. I tried to fix the first error, and was unsuccessful. This is the code I ended up with when I tried to fix the error: <?php if(isset($_FILES['image'])) { $errors = array(); $allowed_ext = array('jpg', 'jpeg', 'png', 'gif'); $file_name = $_FILES['image']['name']; $explode_var = array(); $explode_var[] = '$file_name'; $file_ext = strtolower(end(explode('.', $explode_var ))); $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; if(in_array($file_ext, $allowed_ext) === false) { $errors[] = 'File type not allowed. Contact kalam1ty@linuxmail.org if you want an extension added.'; } if($file_size > 10485760) { $errors[] = 'File too large. Must be under 10MB.'; } if(empty($errors)) { if (move_uploaded_file($file_tmp, 'uploads/images/'.$file_name)) { echo 'File uploaded to kalam1ty.com/uploads/images/'; } } else { foreach ($errors as $error) { echo $error, '<br>'; } } } ?> <html> <head> <title> Kalam1ty - Home </title> <link rel="stylesheet" type="text/css" href="style.css" /> <link href='http://fonts.googleapis.com/css?family=Anonymous+Pro' rel='stylesheet' type='text/css'> <link rel="shortcut icon" href="/home/corona/Development/Web/favicon.ico" type="image/x-icon" /> <meta name="google" value="notranslate"> </head> <body> <div id="EncBG"> <div id="Navbar"> <a href="index.php">Home</a> <a href="downloads.php">Downloads</a> <a href="upload.php">Upload</a> <a href="contact.php">Contact</a> </div> <div id="Enc1"> <div id="Content"> <h2>Upload Images</h2> <form action='upload.php' method='POST' enctype='multipart/form-data'> <p> <input type='file' name='image'> <input type='submit' value='Upload'> </p> </form> </div> </div> <div id="Footer" class="whitetext" align="center"> <b><i> <a href="index.html">HOME</a> // <a href="downloads.html">DOWNLOADS</a> // <a href="#">OTHER</a> </b></i><br> <img src="smlogo.png" alt="Kalam1ty"/> </div> </div> </body> </html> Do you think you can tell me what I did wrong or lead me on the right path? Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1260702 Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2011 Share Posted August 24, 2011 You need to assign the result of the explode to a variable, then use that variable in the remainder of the functions. Original code - $file_name = $_FILES['image']['name']; $file_ext = strtolower(end(explode('.', $file_name ))); Code with stated change - $file_name = $_FILES['image']['name']; $parts = explode('.', $file_name ); $file_ext = strtolower(end($parts)); Quote Link to comment https://forums.phpfreaks.com/topic/245454-help-with-file-upload-system-php-newbie/#findComment-1261426 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.