Jump to content

[SOLVED] copy() problem, not actually copying


chocopi

Recommended Posts

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 ;D

 

~ Chocopi

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ;D

 

~ Chocopi

Link to comment
Share on other sites

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 ;D

 

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 ;D

 

~ Chocopi

 

PS: If anyone has a script which can compare my two codes to see what has actuall changed it would be greatly appreciated

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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 ;D

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.