-1

hi this is the function that upload image inside temp location and save location to session for forther use

    function uploadPhoto()
{
    $rawImage = $_FILES['advPhoto'];
    $uploader = new ImageUploader($rawImage);
    $uploader->moveToProjectTempFolder();

    //1 save the current image in sassion (save the attachment class inside seesion)
    $uploader->saveInSession();
    // $temperrary = $uploader->CurrentImgTemperraryLocation();

    //2 send reponse the current image location
    AjaxHelper::sendAjaxResponse("images/temp/" . $uploader->CurrentImgTemperraryLocation());
    //create image tag and set the image source the "temp uploaded image path"
    // done
    //when the mail form is submitted
    //loop through session array 
    //move the user uploaded/approved images to permanent folder
    //save image information inside DB

}

here is the function that cause problem I wanna move the picture from temp folder to permanent location but the php_move_uploaded_file() doesn't work in my case I don't really know what is the problem please help me if you know what is the problem thnks .

  function saveAdv()
{
    $advTitle = $_POST['advTitle'];
    $advContent = $_POST['advContent'];
    if (!empty($advTitle) && !empty($advContent)) {

        if (DataValidation::isOnlyPersianOrEnglish($advTitle) && 
DataValidation::isOnlyPersianOrEnglish($advContent)) {
            DBconnection::insertRow('ADVERTISEMENT', ['title', 'Advertisement', 'advDate'], 
[$advTitle, $advContent, date('y/m/d h:i:s')]);
            // AjaxHelper::sendAjaxResponse("success");

            $projectTemp = $_SESSION['ADVERTISEMENT']['Img'];
            move_uploaded_file(
                $projectTemp,
                DOC_ROOT . "/images/advertisementImg/"
            );
            AjaxHelper::sendAjaxResponse($projectTemp);
        }
    } else {
        AjaxHelper::sendErrorMessage(AjaxHelper::EMPTY_EMAIL_OR_PASSWORD);
    }
}

I don't get any error I've already debuged many times but no warning and no errors at all and the location of the folders are completely correct and also there is no permission problems.

The move_uploaded_file() works pretty well at first step that I move image from system temp location to my project temp location, but doesn't work when I wanna move the image from project temp location to permanent location.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Hasib Arya
  • 33
  • 6
  • 1
    "Doesn't work" isn't an error message or problem statement. We can't fix "doesn't work" in code, any more than a mechanic can fix a car that "doesn't work", without any other information about the problem. How is it not working? What exactly happens when you run the code? What did you expect to happen instead? Please provide details of error messages, unexpected behaviour etc. – ADyson Nov 06 '20 at 10:44
  • If you aren't seeing any errors or warnings, please ensure your PHP installation is configured to log errors and warnings to a file - see https://stackify.com/php-error-logs-guide/ if you need to know how to set that up. When you've done that, run your code again and check the file for any useful messages. – ADyson Nov 06 '20 at 10:45
  • I don't get any error I've already debuged many times but no warning and no errors at all and the location of the folders are completely correct and also there is no permission problems very confusing , – Hasib Arya Nov 06 '20 at 11:02
  • the move_uploaded_file() works pretty well at first step that I move image from system temp location to my project temp location but doesn't work when I wanna move the image from project temp location to permanent location – Hasib Arya Nov 06 '20 at 11:05
  • Do you have error logging configured, as I indicated above? That's where you should be looking, if you don't see errors. Also, move_uploaded_file will return false if it fails, and you aren't checking for that in your code. – ADyson Nov 06 '20 at 11:18
  • _"doesn't work when I wanna move the image from project temp location to permanent location "_ ...actually I don't understand this comment. To be clear: `move_uploaded_file()` is **only** for moving files which have just been uploaded in a POST request and are stored in the system temp location. As the documentation (https://www.php.net/manual/en/function.move-uploaded-file.php) states, it first checks whether the file is a valid upload file _meaning that it was uploaded via PHP's HTTP POST upload mechanism_ (that's a direct quote from the docs). If it's not valid by that definition, it fails. – ADyson Nov 06 '20 at 11:20
  • So, if you're trying to use move_uploaded_file to copy files from other locations (not the system temp location) which have not been directly uploaded to that location in the current request, then it won't work. Use PHP's general file copying functionality for moving other files around. See https://stackoverflow.com/questions/19139434/php-move-a-file-into-a-different-folder-on-the-server for examples. – ADyson Nov 06 '20 at 11:21
  • You should [edit] the detail ou added in comments into the question, – Haem Nov 06 '20 at 11:42

1 Answers1

1

move_uploaded_file() is only for moving files which have just been uploaded in a POST request and are stored in the system temp location. As the documentation (https://php.net/manual/en/function.move-uploaded-file.php) states, it first checks whether the file is a valid upload file meaning that it was uploaded via PHP's HTTP POST upload mechanism (that's a direct quote from the docs). If it's not valid by that definition, it fails.

So, if you're trying to use move_uploaded_file() to copy files from other locations (not the system temp location) which have not been directly uploaded to that location in the current request, then it won't work. Use PHP's general file manipulation functionality for moving other files around, using the rename() function (see https://www.php.net/manual/en/function.rename.php for details).

ADyson
  • 57,178
  • 14
  • 51
  • 63