move_uploaded_file() Unable to move file from tmp to dir
When move_uploaded_file() returns false and logs "Unable to move file from [tmp] to [dir]," it rarely means the filesystem is full or the destination folder is missing. In 90% of cases, the failure stems from a mismatch between how PHP handles file uploads via AJAX and how that data is stored on the server. The specific combination of an enctype="multipart/form-data" form handling an AJAX request with jQuery's ajaxForm plugin introduces a subtle trap: the file arrives in the expected location, but the logic used to persist it assumes a different input structure.
The core issue in your scenario lies in how you are storing the uploaded file's metadata in user meta. You are attempting to store the entire $_FILES array into a database field using add_user_meta() or update_user_meta(). While PHP allows writing this associative array to a serialized string, it does not automatically move the physical file from the temporary upload directory ($_FILES["profpic"]["tmp_name"]) to your target directory.
The sequence of events is as follows:
- The client sends the file via AJAX. Because
ajaxFormhandles the form submission asynchronously, the browser keeps the multipart stream open until the server responds. - PHP receives the POST request and populates
$_FILES. - Your code checks
$_FILES["profpic"]["error"], which is correct (0 means success). - You call
move_uploaded_file(). This function requires that the source file exists in the system's temporary directory and that the destination path is writable.
However, if you are using WordPress with home_url()."/path/to/dir/", ensure that path/to/dir/ is actually a valid absolute path relative to the web root. A common mistake is calculating the path relative to the script location rather than the document root, leading to an incorrect destination where PHP cannot write the file.
The most critical flaw in your implementation is this block:
if(get_user_meta($_SESSION['userid'], 'user_profile_picture')==""){
add_user_meta($_SESSION['userid'], 'user_profile_picture', $_FILES['profpic']);
}
else{
update_user_meta($_SESSION['userid'], 'user_profile_picture', $_FILES['profpic']);
}
You are serializing the entire $_FILES array into the database. This is functionally useless for file management because the serialized data contains metadata (size, type, temporary name) but not the actual binary content of the image. Furthermore, storing a 5MB+ image as a serialized string inside a MySQL text or mediumtext column is inefficient and prone to corruption if the row grows too large.
To resolve this, you must separate the storage of the file content from the metadata. Do not use $_FILES directly in add_user_meta. Instead, move the file first, then update the user meta with only the filename or a path reference:
$output_dir = home_url() . "/path/to/dir/";
$filename = $_FILES["profpic"]["name"];
$tmp_name = $_FILES["profpic"]["tmp_name"];
// Ensure destination directory exists and is writable
if (!is_dir($output_dir)) {
mkdir($output_dir, 0775, true);
}
if (move_uploaded_file($tmp_name, $output_dir . $filename)) {
// Store only the filename or relative path in user meta
$path = str_replace(home_url(), '', $output_dir . $filename);
if (get_user_meta($_SESSION['userid'], 'user_profile_picture', true) == "") {
add_user_meta($_SESSION['userid'], 'user_profile_picture', $path, true);
} else {
update_user_meta($_SESSION['userid'], 'user_profile_picture', $path, true);
}
echo "Uploaded File : " . $_FILES["profpic"]["name"];
} else {
// Debug: Check why it failed
error_log("File move failed. Source: " . $tmp_name . " | Dest: " . ($output_dir . $filename));
}
Additionally, verify your directory permissions. While 777 works for testing, it is a security risk in production. Ensure the web server user (e.g., www-data on Linux) owns the directory and has write permissions (755 or 775). If the file arrives but cannot be moved, check the error logs immediately before assuming the code logic is at fault.
If you are using Laravel, consider leveraging its built-in storage facade, which handles moving files to the storage directory and linking them to the public folder automatically:
use Illuminate\Support\Facades\Storage;
$destination = Storage::disk('public')->putFileAs(
'uploads/profiles',
$request->file('profpic'),
$filename
);
$user_meta_path = 'uploads/' . $destination;
add_user_meta($_SESSION['userid'], 'user_profile_picture', $user_meta_path, true);
By decoupling the physical file move from the database write and ensuring the destination path is absolute and writable, you eliminate the move_uploaded_file failure.