943,838 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1273
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 20th, 2009
0

Re: Issue with uploading files

The problem you're having is $_FILES['userfile']['tmp_name'] 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 Syntax (Toggle Plain Text)
  1. <?php
  2. $uploads_dir = '/uploads';
  3. foreach ($_FILES["pictures"]["error"] as $key => $error) {
  4. if ($error == UPLOAD_ERR_OK) {
  5. $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
  6. $name = $_FILES["pictures"]["name"][$key];
  7. move_uploaded_file($tmp_name, "$uploads_dir/$name");
  8. }
  9. }
  10. ?>
Source: php.net (move_uploaded_file)

Notice how in that code the destination is /uploads/[name of the actual file]. Should solve your problem.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Jul 21st, 2009
0

Re: Issue with uploading files

Click to Expand / Collapse  Quote originally posted by mschroeder ...
The problem you're having is $_FILES['userfile']['tmp_name'] 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 Syntax (Toggle Plain Text)
  1. <?php
  2. $uploads_dir = '/uploads';
  3. foreach ($_FILES["pictures"]["error"] as $key => $error) {
  4. if ($error == UPLOAD_ERR_OK) {
  5. $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
  6. $name = $_FILES["pictures"]["name"][$key];
  7. move_uploaded_file($tmp_name, "$uploads_dir/$name");
  8. }
  9. }
  10. ?>
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 Syntax (Toggle Plain Text)
  1. <?php
  2. $uploads_dir = "uploads/";
  3.  
  4. foreach($_FILES['userfile']['error'] as $key => $error) {
  5.  
  6. if($error == UPLOAD_ERR_OK) {
  7.  
  8. $tmp_name = $_FILES['userfile']['tmp_name'][$key];
  9. $name = $_FILES['userfile']['name'][$key];
  10. move_uploaded_file($tmp_name, $uploads_dir .$name);
  11. }
  12. }
  13. ?>

The error i got is:
Warning: Invalid argument supplied for foreach() in /home/alanhuno/public_html/upload2.php on line 4
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
Jul 21st, 2009
0

Re: Issue with uploading files

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 Syntax (Toggle Plain Text)
  1. <?php
  2. $uploads_dir = "uploads/";
  3.  
  4. foreach($_FILES['userfile']['error'] as $key => $error) {
  5.  
  6. if($error == UPLOAD_ERR_OK) {
  7.  
  8. $tmp_name = $_FILES['userfile']['tmp_name'][$key];
  9. $name = $_FILES['userfile']['name'][$key];
  10. move_uploaded_file($tmp_name, $uploads_dir .$name);
  11. }
  12. }
  13. ?>

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 Syntax (Toggle Plain Text)
  1. <?php
  2. $uploads_dir = "uploads/";
  3.  
  4. $tmp_name = $_FILES['userfile']['tmp_name'];
  5. $name = $_FILES['userfile']['name'];
  6. move_uploaded_file($tmp_name, $uploads_dir .$name)
  7. or die("could not upload file");
  8. ?>

Thanks to all who tried to help. I hope my hair grows back now
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
Jul 21st, 2009
0

Re: Issue with uploading files

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

html Syntax (Toggle Plain Text)
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Untitled Document</title>
  7. </head>
  8. <body>
  9. <form enctype="multipart/form-data" method="post" action="upload2.php">
  10. Send this file: <input name="userfile" type="file" /><br />
  11. <input type="submit" value="Send File" />
  12. </form>
  13. </body>
  14. </html>

and upload2.php

php Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4. if (move_uploaded_file($_FILES['userfile']['tmp_name'], "/home/alanhuno/public_html/dw/uploads")) {
  5. print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
  6. } else {
  7. print "Upload Failed";
  8. }
  9. ?>




are u working with xamp or wamp ?
this is only the path problem rest of the code is ok.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
basantxbs is offline Offline
12 posts
since Apr 2009
Jul 21st, 2009
0

Re: Issue with uploading files

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.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Jul 22nd, 2009
0

Re: Issue with uploading files

$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";
                }
Quote ...
You can use this code . This will store your file at image directory.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
basantxbs is offline Offline
12 posts
since Apr 2009
Jul 22nd, 2009
0

Re: Issue with uploading files

Click to Expand / Collapse  Quote originally posted by basantxbs ...
$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
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: delete a row/entry form table in mysql using textbox form.
Next Thread in PHP Forum Timeline: Calls to mysqli properties/functions returning errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC