0

i am new to web development. As i go through some tutorial, i found that i was not able to upload a file to my server using php. Below shows whats is in my htdocs directory in apache2.

./index.php ./upload_action.php ./uploads 

index.html

 1 <html> 2 <head> 3 </head> 4 <body> 5 <h1>New Model</h1> 6 7 <h2>Model information</h2> 8 <form enctype="multipart/form-data" action="./upload_action.php" method="POST"> 9 <p> Name: 10 <input type="text" name="textline" > 11 </P> 12 13 <p> Model to upload: 14 <input type="file" multiple name="file to upload" value="fileupload" id="fileupload"> 15 </P> 16 <br> 17 <input type="reset" value="reset"> 18 <input type="submit" value="submit"> 19 </form> 20 21 22 </body> 23 24 </html> 

upload_action.php

 1 <?php 2 $uploaddir = "/uploads/"; 3 $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 4 5 echo '<pre>'; 6 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 7 echo "Success.\n"; 8 } else { 9 echo "Failure.\n"; 10 } 11 12 echo 'Here is some more debugging info:'; 13 print_r($_FILES); 14 print "</pre>"; 15 ?> 

Error msg:

Failure. Here is some more debugging info:Array ( [file_to_upload] => Array ( [name] => test.pdf [type] => application/pdf [tmp_name] => /tmp/phpz2qaQV [error] => 0 [size] => 513536 ) ) 
9
  • What is your file name index.html or index.php Commented Jun 12, 2017 at 5:31
  • 1
    $uploaddir = "/uploads/"; <- That path points to the root folder on your server (the initial slash means "from the root of the current drive") Change to: $uploaddir = __DIR__ . "/uploads/"; You're also missing an equal sign for your "action". Commented Jun 12, 2017 at 5:31
  • @MagnusEriksson i just upadted my error msg, i tried your suggestion but doesn't seems working. Commented Jun 12, 2017 at 5:34
  • do you want to upload multiple file or single file? Commented Jun 12, 2017 at 5:34
  • Make sure that the upload folder is writeable. Check your error log for any potential errors. You should also make sure that your code shows all errors while you're coding: stackoverflow.com/questions/5438060/… Commented Jun 12, 2017 at 5:34

2 Answers 2

1

If you want to upload one file at one time than replace html with this

  • name = "userfile"
  • remove multiple

And it will start working

if you are uploading multiple file at one time then replace html with this then you need to run loop to store file. Refer this

If you facing trouble to upload big file then you can put it on top of your php page

ini_set('upload_max_filesize', '10M'); ini_set('post_max_size', '10M'); ini_set('max_input_time', 300); ini_set('max_execution_time', 300); 
Sign up to request clarification or add additional context in comments.

4 Comments

this code is not comming in my answer so I put here <input type="file" name="userfile" value="fileupload" id="fileupload" />
Yeah, its working now with small file. With bigger file its showing Failure but no error message, i believe it might be due to php.ini config setting? And could you please explain its that a must to change name to "userfile" and why?
You need to change name of your file because in php you are handling file as $_FILES['userfile']['name'] int means type is file, name is "userfile" and param is "name" so you need to change name in html so exactly same name can be posted and no error to retrieve file
Noted. With appreciate your help.
0

You are missing an equals sign. Try this instead:

<form enctype="multipart/form-data" action="./upload_action.php" method="POST"> 

3 Comments

yeah, just found out and updated to my question. From not totally not working to working but getting an error message.
@JefferyLR - "but getting an error message" - If you get an error message, you need to share it.
@MagnusEriksson yeah, exactly the same error msg on question above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.