Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. File upload not working, can't figure out why...

File upload not working, can't figure out why...

Scheduled Pinned Locked Moved Web Development
csharpphphtmldatabaselinux
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DSB Audio David Sweeney Bear
    wrote on last edited by
    #1

    Hi all, first attempt to set up a file upload on web page. Pretty basic stuff, but my first try so can't see why it isn't working. Firstly I'm running on apache2 on a VPS debian 10 install. /etc/php/7.4/apache2/php.ini contains the following:

    ;;;;;;;;;;;;;;;;
    ; File Uploads ;
    ;;;;;;;;;;;;;;;;

    ; Whether to allow HTTP file uploads.
    ; http://php.net/file-uploads
    file_uploads = On

    ; Temporary directory for HTTP uploaded files (will use system default if not
    ; specified).
    ; http://php.net/upload-tmp-dir
    ;upload_tmp_dir =

    ; Maximum allowed size for uploaded files.
    ; http://php.net/upload-max-filesize
    upload_max_filesize = 2M

    ; Maximum number of files that can be uploaded via a single request
    max_file_uploads = 20

    index.html looks like this:

    Select image to upload:

    upload.php as follows:

    1. {
      echo "Sorry, your file is too large.";
      $uploadOk = 0;
      }

    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
    } else {
    echo "Sorry, there was an error uploadi

    D 1 Reply Last reply
    0
    • D DSB Audio David Sweeney Bear

      Hi all, first attempt to set up a file upload on web page. Pretty basic stuff, but my first try so can't see why it isn't working. Firstly I'm running on apache2 on a VPS debian 10 install. /etc/php/7.4/apache2/php.ini contains the following:

      ;;;;;;;;;;;;;;;;
      ; File Uploads ;
      ;;;;;;;;;;;;;;;;

      ; Whether to allow HTTP file uploads.
      ; http://php.net/file-uploads
      file_uploads = On

      ; Temporary directory for HTTP uploaded files (will use system default if not
      ; specified).
      ; http://php.net/upload-tmp-dir
      ;upload_tmp_dir =

      ; Maximum allowed size for uploaded files.
      ; http://php.net/upload-max-filesize
      upload_max_filesize = 2M

      ; Maximum number of files that can be uploaded via a single request
      max_file_uploads = 20

      index.html looks like this:

      Select image to upload:

      upload.php as follows:

      1. {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
        }

      // Allow certain file formats
      if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
      && $imageFileType != "gif" ) {
      echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      $uploadOk = 0;
      }

      // Check if $uploadOk is set to 0 by an error
      if ($uploadOk == 0) {
      echo "Sorry, your file was not uploaded.";
      // if everything is ok, try to upload file
      } else {
      if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
      } else {
      echo "Sorry, there was an error uploadi

      D Offline
      D Offline
      DSB Audio David Sweeney Bear
      wrote on last edited by
      #2

      (groan...) directory wasn't set to be writeable.

      sudo chmod 777 ./uploads

      did the trick.

      Richard DeemingR J 2 Replies Last reply
      0
      • D DSB Audio David Sweeney Bear

        (groan...) directory wasn't set to be writeable.

        sudo chmod 777 ./uploads

        did the trick.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Congratulations - you've just opened up a potential security vulnerability. You allow users to upload files to a folder which is set to allow any user to execute any file in that folder. Guess how long it will take for someone to upload and execute a virus on your server? :doh: https://linuxize.com/post/what-does-chmod-777-mean/[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • D DSB Audio David Sweeney Bear

          (groan...) directory wasn't set to be writeable.

          sudo chmod 777 ./uploads

          did the trick.

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #4

          :laugh:, glad you figured it out. But you shouldn't chmod 777. That directory should only be writable by the Apache process. Also, it should not live in your document root.

          /var/www

          • website.com
            • html
              • index.php
            • uploads
              • some_file.pdf

          Jeremy Falcon

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups