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
        )

)
JefferyLR
  • 401
  • 1
  • 5
  • 16
  • What is your file name index.html or index.php – Rahul Jun 12 '17 at 05: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". – M. Eriksson Jun 12 '17 at 05:31
  • @MagnusEriksson i just upadted my error msg, i tried your suggestion but doesn't seems working. – JefferyLR Jun 12 '17 at 05:34
  • do you want to upload multiple file or single file? – Parag Soni Jun 12 '17 at 05: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: https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – M. Eriksson Jun 12 '17 at 05:34
  • Your title says "html doesn't run my php script" while it clearly does... (the debug info comes from your php script). – M. Eriksson Jun 12 '17 at 05:37
  • @MagnusEriksson Yeah, its running now due to some type at the beginning. Just updated my tittle. Thank you. – JefferyLR Jun 12 '17 at 05:41
  • @ParagSoni Single file at the moment, will consider multiple file in future that's why i add in multiple in the file type function – JefferyLR Jun 12 '17 at 05:42
  • see my answer, and comment. so your file upload can work – Parag Soni Jun 12 '17 at 05:45

2 Answers2

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);
Parag Soni
  • 713
  • 7
  • 14
  • this code is not comming in my answer so I put here `` – Parag Soni Jun 12 '17 at 05:43
  • 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? – JefferyLR Jun 12 '17 at 05:46
  • 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 – Parag Soni Jun 12 '17 at 05:53
  • Noted. With appreciate your help. – JefferyLR Jun 12 '17 at 06:02
0

You are missing an equals sign. Try this instead:

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