Helpful Information
 
 
Category: PHP
Problems with uploader

I'm having problems with my "uploader". The problem is that it's not uploading. I get this error:

Warning: Unable to open 'C:\\WINDOWS\\Desktop\\dude.JPG' for reading: No such file or directory in /home/chatterspics/public_html/register/index.php on line 158


And my code is:



if ($photo != "")
{
// copy the file from the temporary upload position to where you want it
copy($photo, "../users/$username/$username.gif") or die("<b>Error:</b> could not copy file.");

// tell them it worked
echo "";
}
else
{
// tell them to browse for a file
copy("nophoto.gif", "../users/$username/$username.gif");
echo "You must select a file to upload. If there was an error, please email [email protected] with your photo, stating your username and password";
}


Anyone got any ideas on how to do it? I've looked at php.net but I got confused and more lost than I am now.

<?
if($HTTP_POST_FILES['infile']['tmp_name'])
{
if(!copy($HTTP_POST_FILES['infile']['tmp_name'],$new_home_for_image.$HTTP_POST_FILES['infile']['name']))
{
echo "doh";
}else{
echo "whoot!";
}
}
?>



<form action="<?=$_SERVER[PHP_SELF];?>" name="bella" method="post" ENCTYPE="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="150000">
<input type="file" name="infile"><br />
<input type="submit" name="submit" value="submit" style="width:230" width="230">
</form>

I'm not sure that doing just that will work. Try reading over this one.

<?
$dir="uploaded_files"; // Directory name to upload files to
if ($myfile){
$ext = substr ($myfile, -4); // Strip all but the file extension
}
$filetypes = array (".bmp",".jpg",".gif"); // File entensions to accept
if (is_uploaded_file($myfile)){
$filename=$HTTP_POST_FILES['myfile']['name'];
copy ($myfile,"$dir/$filename") or die("Unable to upload file"); // Upload file
}
if (is_uploaded_file($myfile)){
echo "<p style=\"color:green;\">File uploaded sucessfully</p>"; // On Success
}else{
echo "<p style=\"color:red;\">Choose a file</p>"; // Text to display before upload
}
?>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input type="file" name="myfile"><br><br>
<input type="hidden" name="MAX_FILE_SIZE" value="10240000">
<input type="submit" value="Upload">
</form>

I'm wanting to rename the file once it's uploaded and moved to a directory. Which do I change on there to do that?

I want it naming by their username ($username)

Thanks for your help so far

well I can assure you that the one I posted earlier works ..but is_uploaded_file() or move_uploaded_file() are supposed to be safer



<?php
move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], "/place/to/put/uploaded/file".$HTTP_POST_FILES['userfile']['name']);
?>

Back :p

I'm now using your uploader, firepages. I saw in devshed you gave an answer on howto rename the file when it was uploaded:

$int = rename($filename,$newfilename);

Where will I put this in your code?

Sorry for being dumb :o

As SYP}{ER points out you have to get the filetype as well




<?
$user="joe";
if($HTTP_POST_FILES['infile']['tmp_name'])
{
$path=$new_home_for_image.$HTTP_POST_FILES['infile']['name'];
if(!copy($HTTP_POST_FILES['infile']['tmp_name'],$path))
{
echo "doh";
}else{
$type=array('','.gif','.jpg','.png','.swf');
$bits=getimagesize($path);
$newname=$user.$type[$bits[2]];
if(rename($path,str_replace($HTTP_POST_FILES['infile']['name'],$newname,$path)))
{
echo "whoot!";
}
}
}?>


or just rename when copying



<?
$user="joe";
if($HTTP_POST_FILES['infile']['tmp_name'])
{
$path=$new_home_for_image.$HTTP_POST_FILES['infile']['name'];
$type=array('','.gif','.jpg','.png','.swf');
$bits=getimagesize($path);
$newname=$user.$type[$bits[2]];

if(!copy($HTTP_POST_FILES['infile']['tmp_name'],str_replace($HTTP_POST_FILES['infile']['name'],$newname,$path)))
{
echo "doh";
}else{
echo "whoot!";
}
}?>

Thanks :D That worked

I'm getting an error when there's no image uploaded:

Warning: Unable to open 'none' for reading: No such file or directory in /home/chatterspics/public_html/register/index.php on line 189



$new_home_for_image="../users/$username/";
if($HTTP_POST_FILES['photo']['tmp_name'])
{
$path=$new_home_for_image.$HTTP_POST_FILES['photo']['name'];
if(!copy($HTTP_POST_FILES['photo']['tmp_name'],$path))
{
copy("nophoto.gif","../users/$username/nophoto.gif");
}else{
$type=array('','.gif','.jpg');
$bits=getimagesize($path);
$newname=$username.$type[$bits[2]];
if(rename($path,str_replace($HTTP_POST_FILES['photo']['name'],$newname,$path)))
{
echo "";
}
}
}


Line 189 is



if(!copy($HTTP_POST_FILES['photo']['tmp_name'],$path))


I don't understand it, coz the nophoto.gif image is being copied if nothing has been uploaded, and it's also uploaded image the users are sending. :confused:










privacy (GDPR)