[SOLVED] -> copy file upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

[SOLVED] -> copy file upload

Post by ol4pr0 »

my error, tried differant things here, might have screwed up some more, but all i get now is the following error
copy(Apache/htdocs/files/062004/18214354-952.image/gif): failed to open stream: No such file or directory in 48

This is ofcourse the copy part
Any help apreciated

Code: Select all

$uploaddir = 'Apache/htdocs';
	$uploadfile = $uploaddir . $_FILES['userfile']['name'];
	$time = time();
	
	#$dir = date("mY/$time");
	$dir = date("mY", $time);

	if (!is_dir($uploaddir.'/files/'.$dir))
	{
	umask(0);
	mkdir ("files/".$dir, 0777);
	}

	if ($userfile_type == 'image/gif') {
    $type = '.gif';
    }
    if ($userfile_type == 'image/pjpeg') {
    $type = '.jpg';
    }
    if ($userfile_type == 'image/x-png') {
    $type = '.jpg';
    }
	$fileb = date("dHis", $time);
	$filee = rand(0, 999);
	$fn = $fileb."-".$filee;
	$pic = "/files/".$dir."/".$fn.".".$userfile_type;
	$intpic = $dir."/".$fn.".".$userfile_type;

    $newfile = substr($userfile, -9);
    if($userfile_name = '') {
    print("No file was selected!");
    }
    elseif($userfile_size > $MaxFileSize) {
    print("The file to upload is too big");
    }
    elseif (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))
	{
	copy($HTTP_POST_FILES['userfile']['tmp_name'], $uploaddir."/files/".$intpic);

	#copy($_FILES['userfile']['tmp_name'], $uploaddir."/files/".$intpic);

    $global_db = mysql_connect('localhost', 'root');
    mysql_select_db('some', $global_db) or die("Connection error");
   $query = "INSERT INTO test (File, pic, imgname, imgtime) VALUES ('$newfile$type','".$_POST['pic']."', '".$_POST['intpic']."', '".$_POST['time']."')";
    $result = mysql_query($query) or die("ERROR");
Last edited by ol4pr0 on Sat Jun 19, 2004 12:37 am, edited 3 times in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Have a read of http://php.net/manual/en/features.file-upload.php
Notice their form says .. <form enctype="multipart/form-data" .....
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

yea that was a stupid mistake

EDIT: I edited the above script...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try changing $uploaddir = 'Apache/htdocs'; to the full absolute path to wherever the files directory is. Eg $uploaddir = '/the/full/absolute/path/to/htdocs/'; ... and it will need the trailing / if you are doing $uploadfile = $uploaddir . $_FILES['userfile']['name'];

Also check if 'Apache/htdocs/files/062004' exists as i'm not sure that mkdir is working either.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

This worked :)

Code: Select all

<form action="test.php" method="post" enctype="multipart/form-data">
<center><span class=head>
</td></tr>
<tr><td><span class=mes></td><td><input class=input type=file name=file></td></tr>
<tr><td colspan=2 align=right><input class=input type=submit value=submit>
</table>
</form>
<?

$MaxSize = "100";
if (isset($HTTP_POST_FILES['file']['name'])) $file_name = $HTTP_POST_FILES['file']['name'];
	else $file_name = "";
if (isset($HTTP_POST_FILES['file']['size'])) $file_size = $HTTP_POST_FILES['file']['size'];
	else $file_size = "";
if (isset($HTTP_POST_FILES['file']['tmp_name'])) $file_tmp = $HTTP_POST_FILES['file']['tmp_name'];
	else $file_tmp = "";
    
if (($file_name == "")||($file_size == "")||($file_tmp == "")) {
			echo 'nothing selected';
die;
}
      function getextension($filename)
      {
      	$filename 	= strtolower($filename);
	    $extension 	= split("[/\\.]", $filename);
	    $n 		= count($extension)-1;
	    $extension 	= $extension[$n];
	    return $extension;
        }

		$file_type 	= getextension($file_name);
   		if( $file_type!="gif" && $file_type!="jpg" && $file_type!="png"){
			echo 'extension wrong';
        die;
}
        $MaxSize1000 	= $MaxSize*1000;

		if($file_size > $MaxSize1000)
		{
						echo 'filesize';
        die;
}
$time = time();

$dir = date("mY", $time);

$int_path="g:/apache/htdocs/";
if (!is_dir($int_path.'/files/'.$dir))
{
umask(0);
mkdir ("files/".$dir, 0777);
}
$fileb = date("dHis", $time);
$filee = rand(0, 999);
$fn = $fileb."-".$filee;
$pic = "/files/".$dir."/".$fn.".".$file_type;
$intpic = $dir."/".$fn.".".$file_type;


if (is_uploaded_file($HTTP_POST_FILES['file']['tmp_name']))
{
copy($HTTP_POST_FILES['file']['tmp_name'], $int_path."/files/".$intpic);
}
else {
echo "Possible file upload attack. Filename: " . $HTTP_POST_FILES['file']['name'];
}
move_uploaded_file($HTTP_POST_FILES['file']['tmp_name'], $int_path."/files/".$intpic);
#db connecting
require("inc/db.inc");

$query = "INSERT INTO test (pic, imgname, imgtime) VALUES ('".$pic."', '".$intpic."','".$time."')";
mysql_query($query);
echo $query;


?>
Post Reply