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() ;
}
Related
I have a problem with my logs file, in the past, I used fopen($file, "w+"); with the w+ mode to generate and I wrote on the file, and I used fwrite function to write data and header.
With the time I changed the mode to fopen($file, "a"); and I use the fputs function to write the header, and fwrite for writing the data, but right after that the size of logs is being Vvery small compared to old files, and I think that I lose the data.
My question: I think that the write mode, block the code (sometimes) to write on the file, is that can be true? if not, are there any other things I need to check to fix the problem?
Past code :
if (file_exists($file)) {
$handle = fopen($file, "a+");
fwrite($handle, $record);
fclose($handle);
} else {
$handle = fopen($file, "w+");
$header = 'Name;Last Name;Age;User;Comment';
fwrite($handle, $header);
fwrite($handle, $record);
fclose($handle);
}
New Code :
if(!file_exists($file)) {
$handle = fopen($file, 'a');
$headers = ['Name', 'Last Name', 'Age', 'User', 'Comment'];
fputs($handle, implode($headers, ';')."\n");
fwrite($handle, $record);
fclose($handle);
} else {
$handle = fopen($file, "a+");
fwrite($handle, $record);
fclose($handle);
}
Example: On the past, the size of a day file is 71Mo, but right after the modifications the size of day file was 7Ko
On the 000webhost.com server
This is the code
<?php
$my_file = fopen("my_test.txt", 'w');
fclose($my_file );
I thank those who help me in advance
Use the function chmod
http://it2.php.net/function.chmod
$file = "my_test.txt";
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
chmod($file, 0777);
Note* fwrite is optional in your case if you need to enter some data in file
$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.
Hello how do I return the filename with extension using file_get_contents
here is my code :
$lien ="http://www.miawmiaw.com/coolimage.jpg";
$data = file_get_contents($lien);
$fichier = basename($lien);
$fp = fopen("products/".$fichier,"wb");
if (!$fp) exit;
fwrite($fp, $data);
fclose($fp);
so here $fichier should have the file name such as "coolimage.jpg"
you have used $path inside basename($path); instead of this just pass $lien inside the method
$lien ="http://www.miawmiaw.com/coolimage.jpg";
$data = file_get_contents($lien);
$fichier = basename($lien);
$fp = fopen("products/".$fichier,"wb");
if (!$fp) exit;
fwrite($fp, $data);
fclose($fp);
Just use basename()
$filename = basename("http://www.miawmiaw.com/coolimage.jpg");
Example: http://codepad.org/4ajeBuig
I have 100 txt files on my webserver. I have to insert a string e.g. abcdef on the beginning of all of them using php and save them back again. How is this possible ?
Well, google found this.
function prepend($string, $filename) {
$context = stream_context_create();
$tmpname = tempnam(".");
$fp = fopen($filename, "r", 1, $context);
file_put_contents($tmpname, $string);
file_put_contents($tmpname, $fp, FILE_APPEND);
fclose($fp);
unlink($filename);
rename($tmpname, $filename);
}
So you call prepend($string, $filename) for every file and you're done.
Look at opendir, glob, fopen, and fwrite. For example:
foreach (glob("*.txt") as $file) {
$fh = fopen($file, 'c'); //Open file for writing, place pointer at start of file.
fwrite($fh, 'abcdef');
fclose($fh);
}