TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Moving File to Directory with PHP

I have this short, basic code:

<?php
    $username = $_POST['User'];
    $file = $_FILES['File'];
    $size = $_FILES['File']['size'];
    $name = $_FILES['File']['name'];
    $temp_name = $file['tmp_name'];
    $user_id = Data::getUserId($username);
    $target_path = "/---/---/profile_pics/" . $name;

    $sql = "INSERT INTO profile_photos (name, size, user_id) 
               VALUES ('{$name}', '{$size}', '{$user_id}');";
    mysqli_query($dbconnection, $sql);

    move_uploaded_file($temp_name, $target_path);
?>

Note, this is TESTING code so I may have not followed best practice.

I get the correct file information into my MySQL database. But I do not get the actual file moved in the directory.

Also, the database IS extracting the correct file size and file name so something is being passed to PHP that is accurate for sure.


I am passing the file from Java. Here is supplemental info on that end (in case anyone knows about that end; but I do not think the problem is here:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url_select);
File file = new File(imagePath);
HttpResponse response = null;
MultipartEntity entity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
StringBody sb;  

try {
    sb = new StringBody(userName);
    entity.addPart("User", sb);
    entity.addPart("File", cbFile);
    post.setEntity(entity);
    response = client.execute(post);            
} 

Upvotes: 0

Views: 92

Answers (2)

TheLettuceMaster
TheLettuceMaster

Reputation: 15734

It path to double check your path kiddies. That was the answer to my issue. The code otherwise is correct.

Upvotes: 0

user984869
user984869

Reputation: 432

PHP probably does not have permission to write to the destination directory. That would explain why PHP has information about the file, but cannot move it to that location.

You might try writing something simple to a file (fopen, fwrite, etc.) at the same location to verify that the permissions are set correctly.

If you have PHP error logging turned on, you should also see a note about this in the log.

Best of luck.

Upvotes: 1

Related Questions