file open and update functions in ci - php

$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.

Related

Return name of created file from decoded string in php

Hi i have the following code to decode and save image
$photo = $_REQUEST['image'];
$binary = base64_decode ($photo);
header ('Content-Type: bitmap; charset=utf-8');
$file = fopen ('uploads/'.time().'.png', 'wb');
fwrite($file, $binary);
fclose ($file);
This works fine but i will like to get the name of the image after it is written and store that name as a variable to use in the rest of the code where it will be saved in mysql database. I can write to mysql i just need to get the file name. Thanks
You are already naming the file yourself. Just store it in a variable before you write the file.
$filename = time().'.png';
$file = fopen ('uploads/'.$filename, 'wb');

PHP file_exists (overwriting file) not working

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);
?>

PDF not downloading when file is called

I have a database that is storing all my pdf files for a website. The table has columns for the library_item_id, filename(name of the file), mime_type, File size, file_item(the Blob) and I have a php file called download.php. This file is supposed to download the correct file from the database when they user clicks the link. But when the file is downloaded and clicked to open I get Adobe saying it cannot open the pdf. Saying its not decoded correctly. Here is my download.php file:
require_once("scripts/connection.php");
if(isset($_GET["id"])){
$fid = $_GET["id"];
}
else{
header("Location: literature.php");
}
$sql= "SELECT filename, mime_type, file_item FROM library_items WHERE library_item_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $fid);
$stmt->execute();
$stmt->bind_result($filename, $mime, $file_item);
while($stmt->fetch()){
$file_name = $filename;
$mime_type = $mime;
$file = $file_item;
}
header("Content-length: ".strlen($file));
header("Content-type: $mime_type");
header("Content-disposition: download; filename=$file_name");
echo $file;
mysqli_close($conn);
I have tried everything I could think of including add the obj_flush() commands and all that it still gives me the same error. What am I doing wrong on this?
Here is the edit for the code inserting the file into the database.
session_start();
$display = trim($_POST["file-display-name"]);
$company = trim($_POST["companies"]);
$lib_cat = trim($_POST["library-cats"]);
if(empty($display) || empty($company) || empty($lib_cat)){
$_SESSION["errormsg"] = "Required information is missing please fill out all required fields.";
header("Location: ../library.php");
}
else{
$file_name = $_FILES['library-file']['name'];
$tmp_name = $_FILES['library-file']['tmp_name'];
$file_size = $_FILES['library-file']['size'];
$file_type = $_FILES['library-file']['type'];
$fp = fopen($tmp_name, 'r');
$content = fread($fp, filesize($tmp_name));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc()){
$file_name = addslashes($file_name);
}
if(empty($content)){
$_SESSION["errormsg"] = "Required information is missing please fill out all required fields.";
header("Location: ../library.php");
}
else{
require_once("connection.php");
// Insert the logo into the companies photo table
$sql = "INSERT INTO library_items(filename, mime_type, file_size, file_item, display_name, company_id, library_category_id) VALUES(?,?,?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param('sssssss', $file_name, $file_type, $file_size, $content, $display, $company, $lib_cat);
if(!$stmt->execute()){
$_SESSION["errormsg"] = "Failed to add library item: ".mysqli_error();
header("Location: ../library.php");
}
}
unset($_SESSION["errormsg"]);
$_SESSION["successmsg"] = "Library Item successfully added into the database.";
header("Location: ../library.php");
}
UPDATE:
I now have the file downloading and attempting to display once the downloaded file is double clicked to open. It is telling me there is an invalid colorSpace. From what I can tell this is a problem when the file is uploaded into the database. From my upload file posted is there anything I am not doing correctly?
You need following changes. In the insert file
Instead of
$content = addslashes($content);
use
$content = base64_encode($content);
So while downloading, decode it.
$file = base64_decode($file_item);
Replace the following code
header("Content-type: '".$mime_type."'");
header("Content-Disposition: inline; filename='".$file_name."'");
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
readfile($file);
with
header("Content-length: ".strlen($file));
header("Content-type: $mime_type");
header("Content-disposition: download; filename=$file_name");
echo $file;
check if size of downloaded file is correct (matches size of file in database). If it is, you should be debugging the part where the file is inserted into database. If not - check if anything gets printed out before sending file to browser - maybe scripts/connection.php prints something like empty line, error message or BOM?
If you can't find it, try turning output buffering on and call ob_clean() before sending file.
Sorry for my english, I hope it's understandable.

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");

fwrite (php) doesn't always write

Using php fwrite in a symfony2's controller to write a Base64 image, the file not always has been write in the directory.
The file that i write has every time a different name,so there aren't problem for the
rewrite of the same file.
The variable $data has already been decoded.
public function convertAction(Request $request){
$data= $request->request->get('data');
$filen= $request->request->get('filename');
$uploadDir = $this->container->getParameter('upload_tmp');
$file = $uploadDir . $filen;
$fp = fopen($file, 'wb');
fwrite($fp, $data);
fclose($fp);
return new Response() ;
}
fopen and fwrite return false in case of an error. Do you get false from one of these functions when no file is written?
You can try:
public function convertAction(Request $request){
$data= $request->request->get('data');
$filen= $request->request->get('filename');
$uploadDir = $this->container->getParameter('upload_tmp');
$file = $uploadDir . $filen;
$fp = fopen($file, 'w') or die ('Something went wrong'); // That show is an error ocurres The flag for read/write in binary secure mode "wb" does nothing with fopen()
fwrite($fp, $data);
fclose($fp);
return new Response() ;
}

Categories