Dan
Dan

Reputation: 85

PHP is failing to save Base64 string file in a server

My PHP script is failing to save an image file in 000webhost server. Here is my code:

...

$decodedImage = base64_decode($image_encoded);
$mod_image_name = str_replace(' ', '', $image_name);
$path = "images/" . $mod_image_name . ".jpg";       // 'images' folder is inside 'public_html'
$is_written = file_put_contents($path, $decodedImage);

// to verify variables using MySQL
$query = "INSERT INTO `id2-mydb`.`my_table`
(`id`, `image_name`, `image_code`) 
VALUES (NULL, '$image_name', '$image_encoded')";

mysqli_query($conn, $query);

The string image $image_encoded is coming from Android-processed Base64.encodeToString, and Android side and data transfer is working fine. When $image_encoded inserted into MySQL database, I can copy-paste the code in base64decode.org and it produces the needed image.

My 000webhost File Manager folders looks like this:

And FTP transfer is ON.

I'm not sure what I'm missing in the process and why it's not saving an image in routed folder at all, any help would be appreciated.

Upvotes: 2

Views: 118

Answers (1)

RichGoldMD
RichGoldMD

Reputation: 1282

You could do:

$path = __DIR__ . "/../images/" . $mod_image_name . ".jpg";   

Since images is a sibling of php_folder and I assume this script is in the PHP folder.

You should also check the return value for success, and make sure the web process has write permission to images

Upvotes: 1

Related Questions