Issue with uploading files

Thread Solved

Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Issue with uploading files

 
0
  #11
Jul 20th, 2009
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:
  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.
If you're question/problem is solved don't forget to mark the thread as Solved!

-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: Issue with uploading files

 
0
  #12
Jul 21st, 2009
Originally Posted by mschroeder View Post
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:
  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

  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'm gonna live forever, or die trying.

@ A wise man once told me, in order to understand recursion, you first must understand recursion.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: Issue with uploading files

 
0
  #13
Jul 21st, 2009
Originally Posted by leviathan185 View Post
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

  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

  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
@ I'm gonna live forever, or die trying.

@ A wise man once told me, in order to understand recursion, you first must understand recursion.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: basantxbs is an unknown quantity at this point 
Solved Threads: 1
basantxbs basantxbs is offline Offline
Newbie Poster

Re: Issue with uploading files

 
0
  #14
Jul 21st, 2009
Originally Posted by leviathan185 View Post
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

  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

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Issue with uploading files

 
0
  #15
Jul 21st, 2009
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.
If you're question/problem is solved don't forget to mark the thread as Solved!

-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: basantxbs is an unknown quantity at this point 
Solved Threads: 1
basantxbs basantxbs is offline Offline
Newbie Poster

Re: Issue with uploading files

 
0
  #16
Jul 22nd, 2009
$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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: Issue with uploading files

 
0
  #17
Jul 22nd, 2009
Originally Posted by basantxbs View Post
$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
@ I'm gonna live forever, or die trying.

@ A wise man once told me, in order to understand recursion, you first must understand recursion.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 881 | Replies: 16
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC