简体   繁体   中英

Issue uploading Photo to localhost PHP

I am working on a photo upload form. I am taking over from a person who worked on the code some 6 years ago maybe. It's proven to be an arduous process reading through the code. Currently I am trying to figure out the logic for the photo uploading process.

Below is the code called when a user submits a photo to be uploaded. All is well until it gets to this line:

This Code returning FALSE:

if(move_uploaded_file($file['tmp_name'],$uploadPath.'.'.$extension))

It keeps returning FALSE and as a result the photo is never uploaded. However an entry is still made into the Database .

Entire Code:

if($input->get('uploadcvprocess')) {
    $application->setUser($user); //User who uploaded the Photo
    $application->setName(trim($input->get('name')));//Title of the Photo
    $application->setKeywords(trim($input->get('keywords')));//Keywords of the Photo
    $application->setDate(date('Y-m-d'));//Date of the Upload
    $application->setJobAd(new JobAd($input->get('jobad_id')));
    $application->setDraft(0);
    $application->setValidated(1);
    $application->save();

    $allowedExtension = array('jpg','png','gif');
    $uploadPath = PHOTO_UPLOAD_PATH.'photo-'.$application->getId();

    $file = $_FILES['cv'];//Get ref to uploaded file
    var_dump($file);

    if(!empty($file)){//If exists do this....

        $extension = strtolower(substr(strrchr($file['name'], '.'), 1));

        if(in_array($extension,$allowedExtension))//Check it's valid format
        {
            if(move_uploaded_file($file['tmp_name'],$uploadPath.'.'.$extension))//checks file is valid before uploading
            {
                if($oldextension = $application->getCVExtension())
                {
                    $file = PHOTO_UPLOAD_PATH.'photo-'.$application->getId().'.'.$oldextension;
                    if(is_file($file))
                    {
                        @unlink($file);
                        $application->setCVExtension('');
                    }
                }

                //Set the photo here...
                $application->setCVExtension($extension);
                var_dump($application);
                die();
                $application->save();
            }
        }
        else{//Not valid so show error
            $application->remove();
            $_SESSION['latest_photo_query_results'] = '';
            $_SESSION['latest_photo_query'] = '';
            header('Location: dashboard_photos.html?showForm=true&txtMessage=You can only upload images (.gif, .jpg, .png)#form');
            exit;
        }
    }else{ //Else it doesn't so do this....
        $application->remove();
        $_SESSION['latest_photo_query_results'] = '';
        $_SESSION['latest_photo_query'] = '';
        header('Location: dashboard_photos.html?showForm=true&txtMessage=You can only upload images (.gif, .jpg, .png)#form');
        exit;
    }

    // Reset cache
    $_SESSION['latest_photo_query_results'] = '';
    $_SESSION['latest_photo_query'] = '';

    //When the process is done reload the photos page
    header('Location: dashboard_photos.html');
    exit;
}

When I do a var_dump($applicaton) I get this: (PHOTO OBJECT)

object(Photo)[15]
  private 'table' => string 'photo' (length=5)
  private 'name' => string 'Jack 1' (length=6)
  private 'date' => string '2014-07-02' (length=10)
  private 'keywords' => string '' (length=0)
  private 'cvExtension' => string '' (length=0) <----------Will Hold the path to Photo
  private 'draft' => int 0

When I do a var_dump($file) I get this: (PHOTO TO BE UPLOADED)

array (size=5)
  'name' => string 'check-sheet.png' (length=15)
  'type' => string 'image/png' (length=9)
  'tmp_name' => string 'C:\wamp\tmp\php48B9.tmp' (length=23)
  'error' => int 0
  'size' => int 1374

Can anyone see why move_uploaded_file might be returning FALSE ?

From the PHP manual ( PHP: move_uploaded_file - Manual ):

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

In this case, move_uploaded_file() thinks that this is not a valid upload file and returns FALSE with no other output.

I, encountered a similiar problem when trying to setup a web page to upload multiples pdf files.

I check the folder permissions and after that it worked like a charm.

Hope it helps.

First you need to find out where the file is trying to get moved to so somewhere do:

var_dump(PHOTO_UPLOAD_PATH);

Then check that your local web server has the correct permissions to write to that path. It looks like you're on Windows so you need to find that path in Windows Explorer and right click on the folder and go to Properties. There should be a Security tab where you can change the permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM