samedi 10 mars 2018

move_uploaded_file() function doesn't work on server?

I have a website that allows a user to upload an image using PHP move_uploaded_file() and a PHP form. On local development the upload works fine and the image is copied from the users computer into the '/library/uploads' folder within the site directory.

Although once I've put the site on the web server it doesn't add the image to the folder anymore. Here's my code below:

PHP move_uploaded_file() code...

if($_FILES['myfile']['name'])
        {
          //if no errors...
          if(!$_FILES['myfile']['error'])
          {
            //now is the time to modify the future file name and validate the file
            $new_file_name = strtolower($_FILES['myfile']['tmp_name']); //rename file
            if($_FILES['myfile']['size'] > (102400000)) //can't be larger than 100 MB
            {
              $valid_file = false;
              $message = 'Oops!  Your file\'s size is to large.';
            }

            //if the file has passed the test
            if($valid_file)
            {
              //move it to where we want it to be
              move_uploaded_file($_FILES['myfile']['tmp_name'], '/library/uploads/'.$_FILES['myfile']['name']);
              $message = 'Congratulations!  Your file was accepted.';
            }
          }
          //if there is an error...
          else
          {
            //set that to be the returned message
            $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['myfile']['error'];
          }
        }

Form code:

<form class="search-form__form" method="POST" enctype="multipart/form-data">

    <input type="file" name="myfile" /> 

    <input class="search-form__button" name="submit-service" type="submit" value="Submit Service ">


    <div class="search-form__form-message">
      <p class="search-form__error-message"><?php echo $message; ?></p>
    </div>

  </form>

And here's my web server directory: enter image description here

Any help would be greatly appreciated, been stuck on this problem for days now! Cheers.

Aucun commentaire:

Enregistrer un commentaire