chocopi Posted June 26, 2007 Share Posted June 26, 2007 I have a site where you can upload avatars/images whatever people call them nowerdays, but for some reason the copy statement I was using has stopped working but I can't figure out why. I know the code is running through as I can make the errors appear and I have put echos in between them just to double-check. I know 100% that the folder name is correct. Here is the code I am using this code: <form name="edit_profile" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data"> <input type="file" name="avatar" id="avatar" value="" /> <input type="submit" name="submit" id="submit" value="Submit" /> <?php // validation for avatar // set max file size $max_file_size = '10000'; // set allowed extensions (for more, just add a comma then 'image/whatever' $allowed_files = array('image/gif'); // get avatar information // get avatar name $file_name = $_FILES['avatar']['name']; // get avatar size $file_size = $_FILES['avatar']['size']; // get avatar type image/whatever $file_type = $_FILES['avatar']['type']; // get file extension of $file_type this is only if there are more than 1 extension allowed list($blank,$file_extension) = explode('image/',$file_type); // check if there is a selection by checking if either the name, type or size is empty if($_FILES['avatar']['error'] == 4) { echo ""; $errors++; } else { // check file size is not bigger than max if($file_size > $max_file_size) { // if avatar is too big, give error echo "Your file is larger than 10kb.<br />"; $errors++; } // check the avatar has the correct extension if(!in_array($file_type, $allowed_files)) { // if wrong extension, give error echo "You are not allowed to upload that file type.<br />You are only allowed: .gif<br />"; $errors++; } if($errors == 0) { // get old image $old_file = 'avatars/'.$page_id.'.gif'; // check if old avatar exists if(file_exists($old_file)) { // if avatar exists delete it unlink($old_file); } // copy avatar to directory copy ($_FILES['avatar']['tmp_name'], "avatars/".$_FILES['avatar']['name']) or die("Your avatar could not be copied correctly"); // rename the file to the user id so it can be pulled in other files easily rename ("avatars/".$_FILES['avatar']['name'], "avatars/".$page_id.".".$file_extension) or die("Your avatar could not be copied correctly"); } } ?> The annoying thing is that the code I minipulated(manipulated, however you spell it) from still works, yet from what I can see, nothing is different. <html> <body> <br /> <center> <form name="upload" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"> <input type="file" name="avatar" id="avatar" maxlength="60" /> <br /> <input type="submit" name="submit" id="submit" value="Upload" /> <br /> </form> <?php if($_POST) { // set variables $errors = 0; $directory = 'files/'; $max_file_size = '10000'; $allowed_files = array('image/gif'); $file_location = 'avatar/'; $file_upload_name = ''.$page_id.''; // get avatar information $file_name = $_FILES['avatar']['name']; $file_size = $_FILES['avatar']['size']; $file_type = $_FILES['avatar']['type']; echo "name = ".$file_name."<br />"; echo "size = ".$file_size."<br />"; echo "type = ".$file_type."<br />"; if(empty($file_size) or empty($file_name) or empty($file_type)) { echo "You suck<br />"; } else { list($blank,$file_extension) = explode('image/',$file_type); if($file_size > $max_file_size) { echo "Your file is larger than 10kb.<br />"; $errors++; } if(!in_array($file_type, $allowed_files)) { echo "You are not allowed to upload that file type.<br />You are only allowed: .gif<br />"; $errors++; } if($errors == 0) { copy($_FILES['avatar']['tmp_name'], "files/".$_FILES['avatar']['name']) or die ("Could not copy"); echo "Your image has been uploaded"; } else if($errors > 0) { $errors = 0; die(""); } } } ?> </body> </html> Cheers ~ Chocopi Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 I know the code is running through as I can make the errors appear What are these errors you speak of? Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 26, 2007 Author Share Posted June 26, 2007 i mean if i change the file location, which does not exist or if i change the $FILES['avatar']['name'] to $FILES['avatar']['tmp_name'] then it will throw the die message of the copy, so i know the copy is being used. ~ Chocopi Quote Link to comment Share on other sites More sharing options...
bbaker Posted June 26, 2007 Share Posted June 26, 2007 Do you have the directory permissions correct? How about trying move_uploaded_file() instead of copy()? Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 26, 2007 Author Share Posted June 26, 2007 Well i should do seeing, as I have used copy before and the script that listed second works fine. Also, I have already tried move_uploaded_file() but it still gave the same result(nothing). Thanks for the suggestions though ~ Chocopi Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 26, 2007 Author Share Posted June 26, 2007 Ok its started working, but from what i know i havent changed anything except the die() message, so its either been messing me around, or it fixed itself here is the working code: <?php // validation for avatar // set avatar directory // set max file size $max_file_size = '10000'; // set allowed extensions (for more, just add a comma then 'image/whatever' $allowed_files = array('image/gif'); // get avatar information // get avatar name $file_name = $_FILES['avatar']['name']; // get avatar size $file_size = $_FILES['avatar']['size']; // get avatar type image/whatever $file_type = $_FILES['avatar']['type']; // get file extension of $file_type this is only if there are more than 1 extension allowed list($blank,$file_extension) = explode('image/',$file_type); // check if there is a selection by checking if either the name, type or size is empty if($_FILES['avatar']['error'] == 4) { echo ""; $errors++; } else { // check file size is not bigger than max if($file_size > $max_file_size) { // if avatar is too big, give error echo "Your file is larger than 10kb.<br />"; $errors++; } // check the avatar has the correct extension if(!in_array($file_type, $allowed_files)) { // if wrong extension, give error echo "You are not allowed to upload that file type.<br />You are only allowed: .gif<br />"; $errors++; } if($errors == 0) { // get old image $old_file = 'avatars/'.$page_id.'.gif'; // check if old avatar exists if(file_exists($old_file)) { // if avatar exists delete it unlink($old_file); } // copy avatar to directory copy ($_FILES['avatar']['tmp_name'], "avatars/".$_FILES['avatar']['name']) or die("Your avatar could not be copied correctly."); // rename the file to the user id so it can be pulled in other files easily rename ("avatars/".$_FILES['avatar']['name'], "avatars/".$page_id.".".$file_extension) or die("Your avatar could not be copied correctly!"); } } ?> Thanks for your help ~ Chocopi PS: If anyone has a script which can compare my two codes to see what has actuall changed it would be greatly appreciated Quote Link to comment Share on other sites More sharing options...
bbaker Posted June 26, 2007 Share Posted June 26, 2007 i just tried your code on my localhost and it worked fine. one thing though, not part of your problem. Instead of copying the file, then renaming it, why not just rename it during the copy part. instead of copy ($_FILES['avatar']['tmp_name'], "avatars/".$_FILES['avatar']['name']) or die("Your avatar could not be copied correctly"); // rename the file to the user id so it can be pulled in other files easily rename ("avatars/".$_FILES['avatar']['name'], "avatars/".$page_id.".".$file_extension) or die("Your avatar could not be copied correctly"); how about copy ($_FILES['avatar']['tmp_name'], "avatars/".$page_id.".".$file_extension) or die("Your avatar could not be copied correctly"); Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 26, 2007 Author Share Posted June 26, 2007 Cheers mate I shall try it, I was gonna try something like that, but I was wasnt sure it was possible. It kinda of solves my next problem as if two people uploaded an image with the same name at the same time I could have run into problems, so I was thinking about adding an md5 onto the end, but you have sorted it for me Can you please check the original code that I posted as that did not work and I was wondering if it is because of the rubbish free host I am using Many Thank, ~ Chocopi 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.