1

I'm using PHP to create a website for a FileMaker database. What I want to do is upload an image file so it can be accessed by the database.

This is the code I have (which came from a tutorial):

if (isset($_POST['action']) and $_POST['action'] == 'Upload') {

    # set the destination directory for the image
    $image_directory = 'http://mydomain.com/Images/My_Example/';

    # set the temp and dest file names with paths
    $temporary_file = $_FILES['new_image']['tmp_name'];
    $destination_file = $image_directory . $_FILES['new_image']['name'];

    # move file to the images directory
    $result = move_uploaded_file($temporary_file, $destination_file);

    if ($result) {

        # insert the URL into the product record
        $edit = $fm->newEditCommand('Product', $_REQUEST['recid']);
        $edit->setField('Thumbnail URL', $destination_file);
        $edit->execute();

    } else {
        $error_message = nl2br("temporary_file = ".$temporary_file."\n");    
        $error_message.= nl2br("destination_file = ".$destination_file."\n\n");    
        echo $error_message;
        die('There was an error moving the file.');
    }
}

I understand that the function 'move_uploaded_file()' will move the temporary file, but I can't see a statement that actually uploads to the temporary file in the first place. What am I missing, please? (I don't know if this is a red herring!)

But 'move_uploaded_file()' fails, and I get the error message (which I added to try to debug it) and the 'die' message. Is there any way to get an error code, to tell me why it failed?

I'm assured that the relevant directory does have read/write access. And 'mydomain.com' is actually replaced by the real domain in the script.

Any advice gratefully received. I'm a newbie to PHP, so please bear that in mind when answering! Many thanks.

Cheers

4
  • who is the owner of your folder you are trying to upload? Commented Jun 2, 2014 at 12:14
  • If that's all you got, I think you're right that the code actually saving the temporary file is missing. Commented Jun 2, 2014 at 12:16
  • you don't have to give full path. your upload path should start from web root Commented Jun 2, 2014 at 12:27
  • is the folder is accessible to web Commented Jun 2, 2014 at 12:28

4 Answers 4

1

you are using the destination directory path with 'http://mydomain.com/Images/My_Example/', which will not work. in order to save the file, you need to give it absolute path on the server - means if the DOCUMENT_ROOT folder is "/var/www/html/", then you'll need to upload it to "/var/www/html/Images/My_Example'. you can check your DOCUMENT_ROOT path in the $_SERVER['DOCUMENT_ROOT'] variable, with phpinfo() command in php, or in your apache.conf files. also you need to make sure your php has writing permission on the folder you trying to save into.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'm not yet sure what DOCUMENT_ROOT is, or where it is! Yes, it definitely has write permission, so I'm told. I'm not doing this on my own computer, but using a FileMaker/PHP host. I don't think I have access to the things you mention. What I see in Cyberduck is everything that comes after the domain name in the browser. Everything above that I don't have access to.
0

The problem is in this statement :

$image_directory = 'http://mydomain.com/Images/My_Example/';

When you upload file, you cannot give the url in the destination path. You need to use the relative path like:

$image_directory = './Images/My_Example/';

1 Comment

Thanks, that was the perfect answer to what was wrong! :)
0

first of all: try these error messages

the error message is in: $_FILES['upfile']['error']

    switch ($_FILES['upfile']['error']) {
    case UPLOAD_ERR_OK:
        break;
    case UPLOAD_ERR_NO_FILE:
        throw new RuntimeException('No file sent.');
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        throw new RuntimeException('Exceeded filesize limit.');
    default:
        throw new RuntimeException('Unknown errors.');
}

secondly: http://mydomain.com/Images/My_Example/ is not a vaild upload path.

1 Comment

Thanks. Firstly, I've been trying to get hold of a list of the contents of the $_FILES array, but can't find one for love nor money! Thanks for those! Secondly, it would've been useful if you could've said why it wasn't valid. :)
0

http://mydomain.com/Images/My_Example/ is not valid becuase it is a URL path. You need to specify a directory path:

//$image_directory = 'http://mydomain.com/Images/My_Example/';
// change to:
$image_directory = 'path/to/Images/folder/'; 

1 Comment

Thanks!(8 more to go, it says, what does that mean I wonder?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.