psychohagis Posted January 7, 2007 Share Posted January 7, 2007 I have the following scipt to upload avatars which can either be .gifs or .jpegs - and i have a process for checking that but every time i run this script it complains that the file is not a gif or jpeg, even when it is[code]<?phpsession_start();if (!isset($_SESSION['userid']) or $_SESSION['userid'] ==''){exit('You must be signed in to do that');}$username= $_SESSION['username'];if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type']) or eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])) { //handle the file...} else {exit 'You must upload a .gif or .jpeg file';}if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])) { $extension = '.jpg';} else { $extension = '.gif';}$filename = 'avatars/' . $username . $extension;if (is_uploaded_file($_FILES['upload']['tmp_name']) and copy($_FILES['upload']['tmp_name'], $filename)) { echo 'Avatar uploaded succesfully';} else { echo 'Avatar could not be uploaded';}?>[/code] Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 Here's what I use:[code]<?phpsession_start();if($_SESSION['logged_in'] == true) {$file_upload = "true";if ($imgfile_size > 125999) {$msg="Your uploaded file must be less than 125kb.<BR>";$file_upload="false";}if (!($imgfile_type=="image/jpeg" || $imgfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";$file_upload="false";} if($file_upload=="true") {$folder="$_SESSION[user_name]";if(!is_dir("thumbs/".$folder)) {mkdir("thumbs/".$folder,0777);}$uploaddir = 'thumbs/'. $folder ."/";$uploadfile = $uploaddir . basename($_FILES['imgfile']['name']);if(!file_exists($uploaddir.".htaccess")) {copy("thumbs/.htaccess", $uploaddir .".htaccess");}echo '<pre>';if (move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadfile)) { echo "File upload was successful.<br><br><a href=\"$uploadfile\">View: $uploadfile</a>\n";} else { echo "File upload failed.\n";}}if($file_upload=="false") {echo" <h2>Error:</h2><br><br>$msg";}}else {echo" Not Logged In...";}?>[/code]Although mine is set up to first check if the user is logged in and then create a new folder for that user with 777 permissions for storing their pics. Inside that folder I later add a .htaccess file limiting what can be uploaded to that folder and executed from that folder..you may want to just cut that out of there Quote Link to comment Share on other sites More sharing options...
papaface Posted January 7, 2007 Share Posted January 7, 2007 Change this:[code]if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type']) or eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])) { //handle the file...} else {exit 'You must upload a .gif or .jpeg file';}[/code]To:[code]if (($_FILES["upload"]["type"] == "image/jpg") || ($_FILES["upload"]["type"] == "image/gif") || ($_FILES["upload"]["type"] == "image/jpeg") || ($_FILES["upload"]["type"] == "image/pjpeg")) { //handle the file... }else {echo 'You must upload a .gif or .jpeg file';exit;}[/code] Quote Link to comment Share on other sites More sharing options...
psychohagis Posted January 8, 2007 Author Share Posted January 8, 2007 mm nah i get the same problem Quote Link to comment Share on other sites More sharing options...
psychohagis Posted January 8, 2007 Author Share Posted January 8, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 Please don't bump your posts.Are you sure the file is uploading? What happens if you try to echo some info on the file. Is it there? Quote Link to comment Share on other sites More sharing options...
psychohagis Posted January 8, 2007 Author Share Posted January 8, 2007 [quote]Please don't bump your posts.[/quote]y not, it says to do that if your topic is getting answeredI dunno what should i echo? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 Your topic was still on the first few posts. It seemed unnecessary.Try printing out $_FILES["upload"]["type"] and see what it says. Quote Link to comment Share on other sites More sharing options...
psychohagis Posted January 8, 2007 Author Share Posted January 8, 2007 umm no i dont see anything. could it be something wrong with my form:[code]<form action=upload.php method=post enctype=multipart/form-data><input type=hidden name=MAX_FILE_SIZE value=500000 /><p><label>Select file to upload:<input type=file name=uploadedfile /></label></p><p><input type=submit name=submit value=submit /></p></form>[/code] Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted January 8, 2007 Share Posted January 8, 2007 You need to change all instances of [code=php:0]$_FILES['upload'][/code] to [code=php:0]$_FILES['uploadedfile'][/code] as that's the field name in your form.So this: [code=php:0]if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type']){[/code] Would look like this: [code=php:0]if (eregi('^image/p?jpeg(;.*)?$', $_FILES['uploadedfile']['type']){[/code] RegardsHuggie Quote Link to comment Share on other sites More sharing options...
psychohagis Posted January 8, 2007 Author Share Posted January 8, 2007 Ok im now running it as follows:[code]<?phpsession_start();if (!isset($_SESSION['userid']) or $_SESSION['userid'] ==''){exit('You must be signed in to do that');}$username= $_SESSION['username'];if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type']) or eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])) { //handle the file...} else {echo "You must upload a .gif or .jpeg file";exit;}if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])) { $extension = '.jpg';} else { $extension = '.gif';}$filename = 'avatars/' . $username . $extension;if (is_uploaded_file($_FILES['upload']['tmp_name']) and copy($_FILES['upload']['tmp_name'], $filename)) { echo 'Avatar uploaded succesfully';} else { echo 'Avatar could not be uploaded';}?>[/code]So it now runs throught the script and tells me the file is uploaded succesfully, but i can see it in the relevant folder on my server Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 Do you mean you Can't see it?Are you moving it into a folder other than temp? Quote Link to comment Share on other sites More sharing options...
psychohagis Posted January 8, 2007 Author Share Posted January 8, 2007 well it should be in the folder "avatars" as it says which is below where the upload script is Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 Not quite how it works. A file gets uploaded to the temp folder, and you need to move it. Read this tutorial:http://www.tizag.com/phpT/fileupload.php(There's not a good one here on PHP Freaks I can see, if there is someone correct me please!) Quote Link to comment Share on other sites More sharing options...
psychohagis Posted January 8, 2007 Author Share Posted January 8, 2007 but what is this line: [b] copy($_FILES['upload']['tmp_name'], $filename) [/b] for then. I thought that moved it from tempory to $filename which = 'avatars/' . $username . $extension Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted January 8, 2007 Share Posted January 8, 2007 My advice would be to try this code. I've added a few comments too.[code]<?php// Start the sessionsession_start();// Check that the user is authenticatedif (!isset($_SESSION['userid']) or $_SESSION['userid'] ==''){ exit('You must be signed in to do that');}$username = $_SESSION['username'];// While trouble shooting, dump the variable to check values are OKecho "<pre>";print_r($_FILES);echo "</pre>";// Make sure the file that's uploaded is either .gif or .jpgif (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type']) OR eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])){ // Decide what the extension is going to be if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])) { $extension = '.jpg'; } else { $extension = '.gif'; } // Compose the new file name $filename = 'avatars/' . $username . $extension; // Check the file's been uploaded if (is_uploaded_file($_FILES['upload']['tmp_name'])){ // Copy it to its new location if (copy($_FILES['upload']['tmp_name'], $filename)){ echo 'Avatar uploaded succesfully'; } else { echo 'Unable to copy avatar to its new location'; } } else { echo 'Avatar could not be uploaded'; }}else { echo "You must upload a .gif or .jpeg file"; exit;}?>[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 avatars/ would be a subfolder in tempTry your base path/avatars/.$username etc. Quote Link to comment 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.