travelkind Posted November 22, 2009 Share Posted November 22, 2009 I have written this nice little script that allows me to be able to upload a file to a server but I am getting an error. Here is the error: Notice: Undefined index: name1 in /home/content/d/l/a/dlanden/html/beer/do_upload.php on line 6 Warning: copy(/html/beer/uploads/) [function.copy]: failed to open stream: No such file or directory in /home/content/d/l/a/dlanden/html/beer/do_upload.php on line 6 Couldn't copy the file. Here is my code: <? error_reporting(E_ALL); if ($_FILES['img1'] != "") { copy($_FILES['img1']['tmp_name'], "/html/beer/uploads/".$_FILES['img1']['name1']) or die("Couldn't copy the file."); } else { die("No input file specified"); } ?> <HTML> <HEAD> <TILE>Successful File Upload</TITLE> </HEAD> <BODY> <H1>Success!</H1> <P>You sent: <? echo $_FILES['img1']['name']; ?>, a <? echo $_FILES['img1']['size']; ?> byte file with a mime type of <? echo $_FILES['img1']['type']; ?>.</P> </BODY> </HTML> I am a newbie and any advice on this would be much appreciated! Quote Link to comment Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 Sounds like you want move_uploaded_file rather than copy. Quote Link to comment Share on other sites More sharing options...
salathe Posted November 22, 2009 Share Posted November 22, 2009 Wouldn't you want name instead of name1? Quote Link to comment Share on other sites More sharing options...
travelkind Posted November 22, 2009 Author Share Posted November 22, 2009 Okay I changed the copy to move_uploaded_file and changed name1 to name but got the following error: Warning: move_uploaded_file(/html/beer/uploads/4_cats_101.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/d/l/a/dlanden/html/beer/do_upload.php on line 6 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpqPEPat' to '/html/beer/uploads/4_cats_101.jpg' in /home/content/d/l/a/dlanden/html/beer/do_upload.php on line 6 Couldn't copy the file. Here is my updated code: <? error_reporting(E_ALL); if ($_FILES['img1'] != "") { move_uploaded_file($_FILES['img1']['tmp_name'], "/html/beer/uploads/".$_FILES['img1']['name']) or die("Couldn't copy the file."); } else { Any ideas? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 22, 2009 Share Posted November 22, 2009 you have write permissions on that folder? try this: <?php move_uploaded_file($_FILES['img1']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . 'html/beer/uploads/' . $_FILES['img1']['name']); ?> EDIT: adjust the path/to/file as necessary to make sure it's correct. $_SERVER['DOCUMENT_ROOT'] brings you up to your public directory, in your case that's /html/ maybe? if that's the case, this might work: <?php move_uploaded_file($_FILES['img1']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . 'beer/uploads/' . $_FILES['img1']['name']); ?> you can always check this value and tons of others by creating a phpinfo file: phpinfo.php <?php phpinfo(); ?> Quote Link to comment Share on other sites More sharing options...
salathe Posted November 22, 2009 Share Posted November 22, 2009 Does the folder /html/beer/uploads/ exist and does the web server have write permissions to it? Do you not, instead, want to be saving the file to the /home/content/d/l/a/dlanden/html/beer/uploads/ folder? Try move_uploaded_file($_FILES['img1']['tmp_name'], dirname(__FILE__).'/uploads/'.$_FILES['img1']['name']) Edit: or what mrMarcus said. Quote Link to comment Share on other sites More sharing options...
travelkind Posted November 22, 2009 Author Share Posted November 22, 2009 Okay it works!!! Thank you very much for your help. Here is the code that I ended up using: <? error_reporting(E_ALL); if ($_FILES['img1'] != "") { move_uploaded_file($_FILES['img1']['tmp_name'], dirname(__FILE__).'/uploads/'.$_FILES['img1']['name']) or die("Couldn't copy the file."); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.