Hi,

I am trying to write an upload script and everytime i try and upload a file it will give me an error.

Warning: move_uploaded_file(/home/******/public_html/dw/uploads) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/******/public_html/dw/upload2.php on line 3

I have checked that the folder is there as the error suggests that it is not there but it is definately there.

My code is as follows

upload1.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
	<form enctype="multipart/form-data" method="post" action="upload2.php">
    	Send this file: <input name="userfile" type="file" /><br  />
        <input type="submit" value="Send File" />
    </form>
</body>
</html>

and upload2.php

<?php
					
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], "/home/alanhuno/public_html/dw/uploads")) {
		print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
	} else {
		print "Upload Failed";
	}
?>

I've experienced an error message similar to this when dealing with the file system. At the time I had the problem, the solution came in the form of checking the folder permissions to be sure the proper write access existed.

I've experienced an error message similar to this when dealing with the file system. At the time I had the problem, the solution came in the form of checking the folder permissions to be sure the proper write access existed.

Yeah I did check that as well. The folder I am trying to upload to has 0777 (Full Access).

check weather the path given is correct or not.folder permissions are set automatically.

check weather the path given is correct or not.folder permissions are set automatically.

I have also tried using the following for makng sure the path is correct.

$thisDir = getcwd();
$sveToDir = $thisDir ."/uploads";

	if (move_uploaded_file($_FILES['userfile']['tmp_name'], "$saveToDir")) {		print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";	} else {		print "Upload Failed";	}

Try making a path relative to your script like the following:

<?php
					
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], "uploads/")) {
		print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
	} else {
		print "Upload Failed";
	}
?>

Also you can try this altered version:

<?php
					
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], "/home/alanhuno/public_html/dw/uploads/")) {
		print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
	} else {
		print "Upload Failed";
	}
?>

Just note that there needs to be a slash at the end of the path.

Try making a path relative to your script like the following:

<?php
					
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], "uploads/")) {
		print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
	} else {
		print "Upload Failed";
	}
?>

Also you can try this altered version:

<?php
					
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], "/home/alanhuno/public_html/dw/uploads/")) {
		print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
	} else {
		print "Upload Failed";
	}
?>

Just note that there needs to be a slash at the end of the path.

even with the / at the end this is still giving me the same thing. i have contacted my server support and they advsied that the permissions need to be set to 775 as they do not allow 777 access. so i now know that the permissions are correct.

at this point if i use the full path it says its not there
if i use "/uploads/" it says permission denied

I swear i am going to go bald at 24... AHGHHHH

if i use "/uploads/" it says permission denied

That refers to the root when it begins with a slash. So try removing the slash at the beginning so it looks like "uploads/" just like in my sample code.

That refers to the root when it begins with a slash. So try removing the slash at the beginning so it looks like "uploads/" just like in my sample code.

even with out the / at the start is is giving:

Warning: move_uploaded_file(uploads/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/alanhuno/public_html/upload2.php on line 2

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/var/tmp/phpIfPXPI' to 'uploads/' in /home/alanhuno/public_html/upload2.php on line 2
Upload Failed

i even moved all the files to the root folder to see if that would work.

the form to upload the file is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="upload2.php">
    Send this file: <input name="userfile" type="file" /><br />
    <input type="submit" value="Send File" />
</form> 
</body>
</html>

I am pretty sure that is correct too but thought id post just in case.

The problem you're having is $_FILES is an actual filename. your destination is a directory without a file name. its trying to create a file named "uploads", which is a valid filename on a linux system, in your dw folder. Try actually giving the file a filename in the destination path.

Example:

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

Source: php.net (move_uploaded_file)

Notice how in that code the destination is /uploads/[name of the actual file]. Should solve your problem.

The problem you're having is $_FILES is an actual filename. your destination is a directory without a file name. its trying to create a file named "uploads", which is a valid filename on a linux system, in your dw folder. Try actually giving the file a filename in the destination path.

Example:

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

Source: php.net (move_uploaded_file)

Notice how in that code the destination is /uploads/[name of the actual file]. Should solve your problem.

I tried the above code but it gives an error on the foreach line saying that it is an invalid argument?

i tried modifiying the code to suit my form code and still got the same error

<?php
	$uploads_dir = "uploads/";
	
	foreach($_FILES['userfile']['error'] as $key => $error) {
	
		if($error == UPLOAD_ERR_OK) {
			
			$tmp_name = $_FILES['userfile']['tmp_name'][$key];
			$name = $_FILES['userfile']['name'][$key];
			move_uploaded_file($tmp_name, $uploads_dir .$name);
		}
	}	
?>

The error i got is:
Warning: Invalid argument supplied for foreach() in /home/alanhuno/public_html/upload2.php on line 4

I tried the above code but it gives an error on the foreach line saying that it is an invalid argument?

i tried modifiying the code to suit my form code and still got the same error

<?php
	$uploads_dir = "uploads/";
	
	foreach($_FILES['userfile']['error'] as $key => $error) {
	
		if($error == UPLOAD_ERR_OK) {
			
			$tmp_name = $_FILES['userfile']['tmp_name'][$key];
			$name = $_FILES['userfile']['name'][$key];
			move_uploaded_file($tmp_name, $uploads_dir .$name);
		}
	}	
?>

The error i got is:
Warning: Invalid argument supplied for foreach() in /home/alanhuno/public_html/upload2.php on line 4

I managed to get it working when i changed the code to

<?php
	$uploads_dir = "uploads/";
	
	$tmp_name = $_FILES['userfile']['tmp_name'];
	$name = $_FILES['userfile']['name'];
	move_uploaded_file($tmp_name, $uploads_dir .$name)
		or die("could not upload file");
?>

Thanks to all who tried to help. I hope my hair grows back now :P

Hi,

I am trying to write an upload script and everytime i try and upload a file it will give me an error.

Warning: move_uploaded_file(/home/******/public_html/dw/uploads) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/******/public_html/dw/upload2.php on line 3

I have checked that the folder is there as the error suggests that it is not there but it is definately there.

My code is as follows

upload1.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
	<form enctype="multipart/form-data" method="post" action="upload2.php">
    	Send this file: <input name="userfile" type="file" /><br  />
        <input type="submit" value="Send File" />
    </form>
</body>
</html>

and upload2.php

<?php
					
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], "/home/alanhuno/public_html/dw/uploads")) {
		print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
	} else {
		print "Upload Failed";
	}
?>

are u working with xamp or wamp ?
this is only the path problem rest of the code is ok.

That is what I was trying to illustrate as well, that it was a path problem, aka the filename was missing from the path, the code itself was fine. The example i posted was from the manual illustrating path + filename. I apologize for any confusion caused to the op.

$img_name=time().$_FILES['img']['name']; //  allow to upload more than one file with same name     
$temp_file=$_FILES['img']['tmp_name'];
		$dest_path="Image/".$img_name;
		
                if(move_uploaded_file($temp_file,$dest_path)){
                    echo "File is sucessfully uploaded at Image directory";
                }else{
                    echo "Error in uploading file";
                }

You can use this code . This will store your file at image directory.

$img_name=time().$_FILES['img']['name']; //  allow to upload more than one file with same name     
$temp_file=$_FILES['img']['tmp_name'];
		$dest_path="Image/".$img_name;
		
                if(move_uploaded_file($temp_file,$dest_path)){
                    echo "File is sucessfully uploaded at Image directory";
                }else{
                    echo "Error in uploading file";
                }

Thanks that is a great idea, I was just going to check if the file exists and add a version number to it but that sounds a lot easier. thanks

This question has already been answered. Start a new discussion instead.