john.muckley Posted January 24, 2011 Share Posted January 24, 2011 Hey guys!! i'm after a bit of help with a script i am using for simple image upload to server. At the moment the script works fine and will allow upload of JPG files, i want to extend on this to allow GIF and PNG files to be uploaded aswell. This is the script i am using... <?php //?heck 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 600Kb $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"] < 600000)) { //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))) { echo "Upload Complete! You can use the following link in the IMS:" .$newname; } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only .jpg images under 600Kb are accepted for upload"; } } else { echo "Error: No file uploaded"; } // $sessionid=session_id() //$newname=$_SESSION['session_name'] header( 'Location: success.php?newname1='.$filename ) ; ?> Any help would be appriciated!! ta!! jonny Quote Link to comment https://forums.phpfreaks.com/topic/225503-image-upload-script-help/ Share on other sites More sharing options...
mike12255 Posted January 24, 2011 Share Posted January 24, 2011 if (($ext == "jpg") ||($ext == "gif") || ($ext == "png") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 600000)) { you can view information on php's or (||) operator here you should learn all of the operators before starting to code. Quote Link to comment https://forums.phpfreaks.com/topic/225503-image-upload-script-help/#findComment-1164430 Share on other sites More sharing options...
john.muckley Posted January 24, 2011 Author Share Posted January 24, 2011 thats awesome thank you... the GIF's now work, but still no luck with PNG files. whats this bit doing? ($_FILES["uploaded_file"]["type"] == "image/jpeg") Does that restrict the file type or something? Quote Link to comment https://forums.phpfreaks.com/topic/225503-image-upload-script-help/#findComment-1164437 Share on other sites More sharing options...
mike12255 Posted January 24, 2011 Share Posted January 24, 2011 oops, try loosing the /jpeg and i beleive it should work. Quote Link to comment https://forums.phpfreaks.com/topic/225503-image-upload-script-help/#findComment-1164441 Share on other sites More sharing options...
rockstarrem Posted January 24, 2011 Share Posted January 24, 2011 if (($ext == "jpg") ||($ext == "gif") || ($ext == "png") && ($_FILES["uploaded_file"]["type"] == "image/png" || "image/jpeg" || "image/gif") && That should work. If not, try a different syntax. OR just lose it all together for all file types (do not recommend this at all). Quote Link to comment https://forums.phpfreaks.com/topic/225503-image-upload-script-help/#findComment-1164443 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.