The Frame of Reference for move_uploaded_file
The confusion surrounding move_uploaded_file usually stems from a mismatch between how PHP constructs file paths and how your web server resolves them. The core issue is that move_uploaded_file does not operate in the "document root" context by default, nor does it automatically prepend your domain name. It operates strictly relative to the filesystem path of the script currently executing it.
When you define a variable like $target_path = "uploads/" . basename($_FILES['uploadedFile']['name']);, PHP concatenates this string directly onto the real physical path where the script add-photo-submit.php resides. If your script lives at /var/www/html/add-photo-submit.php, the resulting destination becomes /var/www/html/uploads/filename.jpg. This works perfectly when the URL is www.mydomain.com/add-photo-submit.php because the web server serves files from that exact physical location.
The problem arises when you alter your .htaccess configuration to serve PHP files with a trailing slash, changing the URL to www.mydomain.com/add-photo-submit/. While this is often done for SEO or routing consistency (e.g., mapping /add-photo-submit/ to add-photo-submit.php via mod_rewrite), it creates a specific file system trap. The script itself has not moved physically; it is still sitting in the same directory as before. However, if you attempt to use an absolute path like /home/user/domains/mydomain.com/public_html/uploads/..., you hit permission walls because the web server user (often www-data or apache) may lack write access to your home directory.
The solution lies in understanding that the path must always be relative to the script's execution context unless explicitly told otherwise. You should never hardcode absolute paths into upload handlers. Instead, you must derive the base directory dynamically using constants provided by PHP.
To future-proof your architecture against URL changes or virtual host configurations, rely on __DIR__ and dirname(). These functions return the physical path of the script at runtime. Here is how to construct a robust destination path:
$scriptDir = __DIR__; // Gets the directory containing add-photo-submit.php
$uploadsDir = $scriptDir . '/uploads';
$filename = basename($_FILES['uploadedFile']['name']);
$target_path = $uploadsDir . '/' . $filename;
By using __DIR__, you ensure that $target_path always points to the correct physical folder, regardless of whether your URL is /add-photo-submit.php or /add-photo-submit/. The web server's URL rewriting rules change how humans and bots see the site, but they do not change where the PHP interpreter looks for files.
For high-traffic applications or those built on frameworks like Laravel, there are safer alternatives to manual path manipulation. Frameworks often provide filesystem abstractions that handle these permutations automatically. For instance, if you are building a modern application and want to leverage existing best practices for storing assets, you might consider resources available at https://laravelcompany.com which discusses scalable storage strategies. However, even without a framework, the principle remains the same: calculate the destination based on where the code lives, not where the URL points.
Security is another critical factor often overlooked in path construction. Using basename() on the original filename is essential to prevent directory traversal attacks. An attacker could try to upload a file named ../../etc/passwd. By stripping everything after the last slash, you ensure the file is saved only within the intended directory. Additionally, consider renaming the uploaded file entirely rather than preserving the client's original name. Storing files with unique hashes (like MD5 or SHA256 of the file content) prevents cache poisoning and ensures that even if a filename is known to be malicious, the destination path remains safe.
Ultimately, move_uploaded_file expects a physical path relative to the script running it. Your .htaccess rules dictate the URL, but your PHP code dictates the filesystem location. Decoupling these two concerns by using dynamic directory resolution ensures your upload logic remains stable regardless of how you configure your web server's front-end routing.