how to save image in the folder - php

Hi every on i write a code to save title and image name in the database but in my php code, i can save title text and image name in database but the image cannot be save in the folder(uploadedimages),
here is my code:
<?php
include 'config.php';
$title = $_POST['title'];
$base=$_REQUEST['image'];
$filename = $_REQUEST['filename'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploadedimages/'.$filename, 'wb');
fwrite($file, $binary);
fclose($file);
mysql_query("insert into test_upload(title,img) values('$title','$filename' )");
echo "[".$title."]send";
?>

Add the following code
$filename = $_FILES['filename']['name'];
if (move_uploaded_file($_FILES['filename']['tmp_name'], 'uploadedimages/.'$filename))
{
echo "upload succeed";
} else {
echo "Upload failed";
}

Related

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

Can not upload and display image in webpage with php code

I am facing some problem when I try to upload and display an image with the following php code:
<?php
header('Content-Type: bitmap; charset=utf-8');
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
?>
The image is uploaded and located in the same folder which has the php script.
Now, if I delete all of the code and replace with just 1 line
<?php
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";
?>
The image which has been uploaded, is displayed in the webpage ok.
Then, I combine both of it (uploading and displaying the image).
<?php
header('Content-Type: bitmap; charset=utf-8');
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";
?>
Uet this does not display the image, and it seems that the image has not been uploaded yet.
What is the problem and how can I solve it? I would like to upload the image and show the image on the webpage.
There is no image when you refresh the page because in that case $base=$_REQUEST['image']; will be empty (when you refresh the page you are not providing it with img upload data.)
So basically it overrides the uploaded_image.jpg with empty data.
Just check if you have provided a value for the image. In your case:
if(!empty($_REQUEST['image'])) {
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
}
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";

How to save many images through a script php

In my app android, I take photos with android camera and I send these photos to pc through this script PHP :
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>
I obtain the photo correctly to my pc, but when I send an other photo, this overwrite the previous photo..because the name ("uploaded_image.jpg") is the same.
It's possibile to assign different names so I can save all the photos ?
You can use uniqid to create a unique file name each time,
$fileName = uniqid().'.jpg';
$file = fopen($fileName , 'wb');
you can use PHP time() function. It will add timestamp with photo name.
$file = fopen(time().'uploaded_image.jpg', 'wb');
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen(time().'uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>

how to save the image with name posted in the request

I have this simple php file that read the image from my android application:
<?php
$base=$_REQUEST['image'];
$cod=$_REQUEST['cod_anagrafico'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
//binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
print($binary);
$theFile = base64_decode($image_data);
$file = fopen('docs/test2.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=docs/test.jpg>';
?>
But it save my pic with the standard name "test". So i need to save this image with the name of variable "cod". How can i do that? And how can i delete the image when the process end?
Thanks
Change this line and you should be ok
$file = fopen("docs/{$cod}", 'wb');

Error FileType after inserting a file as Blob - PHPMySQL

I made a simple script to insert files as BLOB (mediumblob) in MySQL Database.
The script works fine, the file is uploaded and saved into the table but when I download the file and I try to open it, it says: "File type HTML document (text/html) is not supported"!
This means there was an error while saving the file's type!
Here is my code, please tell me what can be wrong in it:
upload.php :
if (isset($_POST['upload']))
{
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$p = $cnx->prepare('INSERT INTO commandes (name, size, type, content) VALUES(:name, :size, :type, :content)');
$p->execute(array('name'=>$fileName, 'size'=>$fileSize, 'type'=>$fileType, 'content'=>$content));
echo "<br>File $fileName uploaded<br>";
}
}
Download.php :
$p = $cnx->prepare('SELECT cmd_id, name FROM commandes');
$p->setFetchMode(PDO::FETCH_OBJ);
$p->execute();
if($p->rowCount() == 0)
{
echo "0 Element <br />";
}
else
{
while($data = $p->fetch())
{
?>
<?php echo $data->name;?> <br>
<?php
}
}
if(isset($_GET['id']))
{
$id = $_GET['id'];
$q = $cnx->prepare('SELECT * FROM commandes WHERE cmd_id = :cmd_id');
$q->setFetchMode(PDO::FETCH_OBJ);
$q->execute(array('cmd_id'=>$id));
while($getFile = $q->fetch())
{
header("Content-length: $getFile->size");
header("Content-type: $getFile->type");
header("Content-Disposition: download; filename=$getFile->name");
echo $getFile->pdf;
exit;
}
}
Thank you!
What is the output for the response headers?
Can you ensure that "Content-type" is "Content-Type"
Also, using a debugger to inspect the response is really valuable.
http://fiddler2.com/get-fiddler

Categories