PHP file_exists (overwriting file) not working - php

I followed the answer on the question about uploading images and overwriting the existing file.
The suggested answer is to use the method file_exists(). I used the same method but the image is not overwriting the existing image when I tried to upload a different image but the same filename.
here is my php code:
<?php
$con = mysqli_connect("127.0.0.1","root","","japorms");
$user_id = $_POST["user_id"];
$base_picture = $_POST["base_picture"];
$binary = base64_decode($base_picture);
if (file_exists('user_img/'.$user_id.'.jpg'))
{
unlink('user_img/'.$user_id.'.jpg');
}
$file = fopen('user_img/'.$user_id.'.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
$response["success"]=true;
echo json_encode($response);
?>
SOLVED
I rearranged and fixed my code. it's working now. thanks for all your suggestions.
<?php
$con = mysqli_connect("127.0.0.1","root","","japorms");
/*$con = mysqli_connect("mysql.hostinger.ph","u989983246_jap","japorms","u989983246_jap");
*/
$user_id = $_POST["user_id"];
$base_picture = $_POST["base_picture"];
$binary = base64_decode($base_picture);
if (file_exists('user_img/'.$user_id.'.jpg'))
{
unlink('user_img/'.$user_id.'.jpg');
$file = fopen('user_img/'.$user_id.'.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
}
else{
$file = fopen('user_img/'.$user_id.'.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
}
$response["success"]=true;
echo json_encode($response);
?>

Related

Finding a file with PHP

I want to find a file with a wildcard in the same directory as my index.php is.
When I assign the $file_name manually with the name string, it works fine.
<?php
$file_name = glob("*.csv");
$handle = fopen($file_name, "r");
$file = fread($handle, filesize($file_name));
fclose($handle);
echo $file;
?>
The browser should output the content of the .csv file, like when I assign the $file_name manually.
you need a loop because glob() returns an array:
foreach (glob("*.csv") as $filename) {
$handle = fopen($filename, "r");
$file = fread($handle, filesize($filename));
fclose($handle);
echo $filename;
}
This script will find some images in working folder.
<?php
$workdir = getcwd(); // my working dir
$patternTofind = ".{jpg,gif,png}"; // Images example
$files = glob("$workdir*$patternTofind", GLOB_BRACE);
// Print result found
print_r($files);
?>

How To Map Folder Path For Writing In Php - Uploading

I'm having trouble with uploading an image to my local server. I test it in POSTMAN.
I doubt it is my $path that causes the error.
My upload.php
header('Content-type : bitmap; charset=utf-8');
if(isset($_POST["encoded_string"])){
$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];
$decoded_string = base64_decode($encoded_string);
$path = 'C:/Users/Luffy/Documents/XAMPP/htdocs/images/'.$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
if($is_written > 0) {
$connection = mysqli_connect('localhost', 'root', 'password','photos');
$query = "INSERT INTO photos(name,pathFile) values('$image_name','$path');";
$result = mysqli_query($connection, $query) ;
if($result){
echo "success";
}else{
echo "failed";
}
mysqli_close($connection);
}
}
How do I exactly map folders in Xammp? I tried getting $_SERVER['DOCUMENT_ROOT'] but it doesn't working.

Create folder and save image into this folder that is send from android application

I have following PHP code to create a folder and save an image into this generated folder. What works is the generation of the folder, but the associated image will not be inserted into the folder, so the folder is always empty:
$uid = $_POST['uid'];
$image = $_POST["image"];
$suffix = $db->createRandomID(); //function creates random numbers and stores in $suffix
$url = "http://XXX.XXX.XXX.XX/uploads_offer/";
$image_name = "img_offer_".$suffix."".$uid."_".date("Y-m-d-H-m-s").".jpg";
$path = $url."".$image_name; // path of saved image
// base64 encoded utf-8 string
$binary2 = base64_decode($image);
// binary, utf-8 bytes
header("Content-Type: bitmap; charset=utf-8");
$filepath = $image_name;
if (file_exists("../uploads_offer/".$uid)){
//$file = fopen("../uploads_offer/".$uid . $image_name, "wb");
$file = fopen("../uploads_offer".$uid.$image_name, "wb");
fwrite($file, $binary2);
fclose($file);
}else{
$result8 = mkdir("../uploads_offer/".$uid, 0755);
$file = fopen("../uploads_offer".$uid.$image_name, "wb");
fwrite($file, $binary2);
fclose($file);
}
You are missing slashes (/) in your fopen() calls:
$file = fopen("../uploads_offer/".$uid."/".$image_name, "wb");

file open and update functions in ci

$result = $this->db->insert('user_reg', $user_data);
if($result)
{
$userid = $this->db->insert_id();
$this->db->where('user_id', $userid);
$user_data = $this->db->get('user_reg')->row();
// $userid = $this->db->insert_id();
chmod('./upload/', 0777);
$path = './upload/'.$userid;
$binary=base64_decode($user_profile_img);
header('Content-Type: bitmap; charset=utf-8');
$img_path=$path.'/profile_image.jpg';
$file = fopen($img_path, 'wb');
fwrite($file, $binary);
$Gimage = mysql_real_escape_string(base_url($path.'/profile_image.jpg'));
fclose($file);
i am getting error saying fclose is using binary can anyone tell what is the solution and i am not able to understand with ci update function.i need to update image path in the table without changing other contents.
You have to check that the file is created correctly. Please add the following check:
if (!$file) die ('Error opening file for writing');
after the line:
$file = fopen($img_path, 'wb');
and if you get the message now, verify the generated file name and permissions.

Create file in directory

I am trying to create a file in a sub directory but it is not creating it
$tempFilename = "xml/tempfile.xml";
if(file_exists($tempFilename))
{
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
$fileTemplate = ($xml);
You can use file_put_contents
file_put_contents($tempFilename, '23');
Use fwrite :
$tempFilename = "xml/tempfile.xml";
// Test if directory exists.
$dirname = dirname($tempFilename);
if (!is_dir($dirname))
{
mkdir($dirname);
}
if(file_exists($tempFilename)){
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
// Write what you need
fwrite($file, 'content of my file');
fclose($file);
try this
first you need to create directory
$path = '/xml';
mkdir($path, 0777, true);
$tempFilename = "/xml/tempfile.xml";
if(file_exists($tempFilename))
{
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
fwrite($file, '23');
fclose($fp);

Categories