PHP upload image

PHP upload image

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving PHP Image Upload Errors and Security Pitfalls

Getting a PHP form to handle file uploads is a common milestone for developers, but it often introduces significant complexity compared to standard text inputs. The error you encountered—Fatal error: Call to undefined function file_get_content()—is the immediate blocker, but fixing just that line leaves your application vulnerable to critical security issues and logical bugs if not addressed properly.

The first step is correcting the function name. In PHP, the correct built-in function for reading a file's contents into a string is file_get_contents(), with an underscore, not a hyphen. The typo file_get_content does not exist in the core language, which is why the interpreter throws a fatal error immediately upon execution. However, simply renaming this function will likely result in new errors because of how you are handling the data.

Reading binary image data directly into a string using file_get_contents() works for small files, but it requires careful handling of the resulting variable. In your specific implementation, you applied addslashes() to the image content: $image = addslashes(file_get_content($_FILES['image']['tmp_name']));. This is problematic for two reasons. First, as mentioned, the function name was incorrect. Second, and more importantly, adding SQL escape characters like backslashes directly to binary data corrupts the file. If you attempt to store a JPEG or PNG blob in a database using a text-based escaping method, you will destroy the image's header information, rendering it unreadable by any browser once retrieved.

Furthermore, your SQL query relies on the deprecated mysql_query() function and the mysql_* extension, which has been removed from PHP since version 7.0. While legacy codebases may still use this, modern development requires a switch to either MySQLi or PDO. Additionally, mysql_query() returns a resource that can be tricky to handle with multiple parameters without using mysql_real_escape_string(), and even then, it is not recommended for new projects due to the risk of SQL injection.

To implement a secure image upload system today, you must follow a specific workflow that prioritizes validation over storage in the database itself. Unless you have a very specific requirement to store images within the MySQL BLOB column, it is standard practice to write the uploaded file directly to the server's filesystem using move_uploaded_file(). This approach is more efficient for large files and keeps your database schema cleaner.

If you strictly require storing the image data in a database column (as implied by your INSERT INTO content statement), you must skip any text-based escaping and use prepared statements to insert the binary data safely. Here is how the logic should be structured using PDO, which provides superior security against SQL injection compared to string concatenation:

$stmt = $pdo->prepare("INSERT INTO content (first_name, last_name, /* ... */, image_name, image_data) VALUES (:first_name, :last_name, /* ... */, :image_name, :image_data)");

// Ensure the file is an actual image before processing
$imageInfo = getimagesize($_FILES['image']['tmp_name']);
if ($imageInfo === false) {
    die("That isn't a valid image.");
}

// Read binary content without adding slashes
$imageData = file_get_contents($_FILES['image']['tmp_name']);
$imageName = $_FILES['image']['name'];

// Execute with bound parameters to prevent SQL injection and preserve binary data
$stmt->execute([
    'first_name' => $firstName, // Get from $_POST
    'last_name'  => $lastName,  // Get from $_POST
    'image_name' => $imageName,
    'image_data' => $imageData
]);

This approach ensures that the binary image data remains intact. The trade-off here is storage size; database BLOBs can be expensive in terms of disk space and query performance compared to storing files on a filesystem or an object store like Amazon S3. However, if your current architecture demands DB storage, this method respects the integrity of the file format while maintaining security standards. Always validate the MIME type using finfo_file() before processing to ensure only allowed image types are accepted.

Our network