0

I am using GoDaddy for my hosting account and I am trying to get a file to upload correctly.

Here is my PHP..

<?php

    $listingId = $_POST['listingId'];

    $upload_path = 'public_html/_/hostname.com/stash/images/';

    $upload_url = 'https://www.hostname.com/stash/images/';

    //response array
    $response = array();


    if($_SERVER['REQUEST_METHOD']=='POST'){

        //checking the required parameters from the request
        if(isset($_POST['name']) and isset($_FILES['image']['name'])){

            //getting name from the request
            $name = $_POST['name'];

            //getting file info from the request
            $fileinfo = pathinfo($_FILES['image']['name']);

            //getting the file extension
            $extension = $fileinfo['extension'];

            //file url to store in the database
            $file_url = $upload_url . $listingId . '.' . $extension;

            //file path to upload in the server
            $file_path = $upload_path . $listingId . '.'. $extension;

            //trying to save the file in the directory
            try{
                //saving the file
                move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
            $response['message'] = "idk if this worked";
            //if some error occurred
            }catch(Exception $e){
                $response['error']=true;
                $response['message']=$e->getMessage();
            }
            echo json_encode($response);

        }else{
            $response['error']=true;
            $response['message']='Please choose a file';
        }
    }
?>

The problem I am facing is that its not uploading the file to the specific folder. The folder/location is 777.

I tried using postman to send a request and it's not working. Any help would be greatly appreciated.

letsCode
  • 2,774
  • 1
  • 13
  • 37
  • Please post the rendered HTML of your `
    ` element that makes the `POST` request.
    – Dai Dec 02 '19 at 03:15
  • Without an error message, any answers were solely be guesses – Machavity Dec 02 '19 at 03:15
  • whats the point if you can send a request from postman @Dai – letsCode Dec 02 '19 at 03:16
  • I guess im wondering what the path would be for godaddy, if anyone else had this issue. im not getting errors. – letsCode Dec 02 '19 at 03:16
  • Incidentally, that's not how you check for errors with `move_uploaded_file`. See [this answer](https://stackoverflow.com/a/30904093/2370483) for how to do that – Machavity Dec 02 '19 at 03:20
  • 1
    @soldfor Because you may have an incorrect `enctype=""` attribute. You have to use `multipart/form-data` when uploading files, not `x-www-form-urlencoded` (which is the default). – Dai Dec 02 '19 at 03:29

0 Answers0