I have created an API so android application can connect to the website the problem I am facing is I am unable to provide access to provide API to upload image to the server as of now I have created so image would be saving as base64 code but the main problem is that when every API call is made and image uploading it is giving me an error of image not found unable to load file stream. Here is the error I am Getting
<b>Warning</b>: fopen(uploads/testimg.png): failed to open stream: No such
file or directory in <b>/home/begazed/public_html/api/classfiles/Photo.php</b> on line <b>87</b><br />
<br />
<b>Warning</b>: fwrite() expects parameter 1 to be resource, boolean given in <b>/home/begazed/public_html/api/classfiles/Photo.php</b> on line <b>88</b><br />
<br />
<b>Warning</b>: fclose() expects parameter 1 to be resource, boolean given in <b>/home/begazed/public_html/api/classfiles/Photo.php</b> on line <b>89</b><br />
{"result":1,"message":"Image upload complete, Please check your php file directory"}
This is the function I have created to upload image to the server
function upload_photo($connect, $base, $filename) {
$binary = base64_decode($base);
$query = $connect->prepare("INSERT INTO test_photo (filename) VALUES (:filename)");
$query->execute(array(':filename' => $filename));
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploads/'.$filename, 'wb');
fwrite($file, $binary);
fclose($file);
$err['result'] = 1;
$err['message'] = 'Image upload complete, Please check your php file directory';
return json_encode($err);
}
The main api file from where the request is handled
switch($action)
{
case 'upload_photo':
$base = $_REQUEST['image'];
$filename = $_REQUEST['filename'];
echo $retval = $photo->upload_photo($connect, $base, $filename);
break;
default:
echo "Invalid Request";
break;
}
tthis is the url used to make a call
www.domainname.com/api/api.php?action=upload_photo&fimename=(filename)&image=iamge nae
Can anyone help me out to fix this error
If your PHP-file is included from another file, located somewhere else, then the code will use that file path as basis.
Include basically "copys" the content of the included file into into your script, which makes the paths relative from the parent script, not the script that's included.
To fix this, you can use the magic constant __DIR__, which returns the absolute path to the file itself.
So changing:
fopen('uploads/' . $filename, 'wb')
to
fopen(__DIR__ . '/uploads/' . $filename, 'wb')
should solve your issue.
Related
I am trying to upload an image from android to mysql
Now the path can be successfully uploaded but there's something wrong on this code
<?php
require_once '../database/database.php';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// base_64 encoded string from android
$imageData = $_POST['image'];
// edittext from android
// $imageName = $_POST['image_name'];
$path = "images/Sample.jpg";
$actualPath = "http://192.168.254.123/*****/admin/$path";
if($user->UploadFiles($actualPath))
{
file_put_contents($path, base64_decode($imageData));
echo "Success";
}
else
{
echo "Failed";
}
}
?>
On the line where file_put_content is the error . here's the full error
Warning file_put_contents(images/Sample.jpg):failed to open stream:No such file or directory in C:\xampp\htdocs\projectname\admin\apk_api\upload_profile.php on line 17
Here's the proof that I can actually save the path on my database
and here's my directory
Can someone please help me out.
The directory images/ does not exists from the point of view, where the admin\apk_api\upload_profile.php file is. The resulting directory path will be
C:\xampp\htdocs\projectname\admin\apk_api\images\
However, the target directory should be
C:\xampp\htdocs\projectname\admin\images\
Create a relative path based from the location of the admin\apk_api\upload_profile.php file. You have to use something like ../images/ to get to the target directory.
I am trying to generate and scaled image for thumbnails, scaling image has no problem and when use
image_save($_image) // Works Fine
(using the same folder an replacing) this code has not problem, the problem starts when I tried to save images in another destination folder using destination parameter:
image_save($_image, $destination) //throws errors
then the error occurs:
Warning: imagejpeg(C:\xampp\htdocs\drupal-devel\sites\default\files): failed to open stream: Permission denied in image_gd_save() (line 284 of C:\xampp\htdocs\drupal-devel\modules\system\image.gd.inc).
I'm working into a Windows xampp and using the function is_write of PHP, return true, no problem with permissions.
I had been trying for a time and I don't know what's happening, no problem with permissions.
This is my code:
$destination = 'public://gallery/thumbs/';
$filepath = drupal_get_path('module', 'gallery_blueprint') . '/img/large/1.jpg'
// Someplace before function
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
_generate_thumb($filepath,$destionation,300,NULL);
// Function code
function _generate_thumb($filepath, $destination_path, $width = NULL, $height = NULL) {
$_img = image_load($filepath);
$scaled = image_scale($_img, $width, $height); // Return true
$result = image_save($_img, $destination_path); //Error occurs whit destination path
return $result;
}
Is an stupid answer but will be written.
the function image_save() when you pass throw the $destination folder, it doesn't write the name by default, finally you need to write a final absolute name path, the name of file is required or throws.
Warning: imagejpeg(C:\xampp\htdocs\drupal-devel\sites\default\files): failed to open stream: Permission denied in image_gd_save() (line 284 of C:\xampp\htdocs\drupal-devel\modules\system\image.gd.inc).
//finally you need to write whole filepath
image_save($_img, $finalname_path) //$filname_path = public://gallery/thumbs/1.jpg
The path of my .txt file is C:\Users\George\Desktop\test.txt
And I use:
$path = "C:\\Users\\George\\Desktop\\test.txt";
$fileContent = file_get_contents($path);
echo $fileContent;
But I get file_get_contents(C:\Users\George\Desktop\test.txt) [function.file-get-contents]: failed to open stream. But why?
Use this code:
$path = "C:/Users/George/Desktop/test.txt";
$fileContent = file_get_contents($path);
echo $fileContent;
as the error mentions, that path exists...
Wrong assertion, PHP is just telling you there was an error opening that path, it doesn't mean it exists, also, the error message should mention the reason for the error, i.e.: not found, permission denied, etc...
Answer:
Your code syntax is correct. The error is one of the following 3:
1 - The file doesn't exist.
2 - The path is wrong.
3 - Php doesn't have permission to access that file.
Maybe if YOU do not have the right to read this file as PHP, the system have it, try exec or system :
$path = 'C:\\Users\\George\\Desktop\\test.txt';
function getFile_exec($path)
{
if(file_exists($path))
{
return exec("cat $path");
}
else
{
return false;
}
}
function getFile_syst($path)
{
if(file_exists($path))
{
return system("cat $path");
}
else
{
return false;
}
}
var_dump(getFile_exec($path));
var_dump(getFile_syst($path));
But as you have this error : Warning: file_get_contents(C:/Users/George/Desktop/test.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/a2133027/public_html/index.php on line 5 and like Pedro Lobito said, you must be on linux, so, the good way to access your files on desktop may be : ~/Desktop/test.txt or ~/test.txt if it's directly in your user files ... Are you sure, you are under windows ?
I'm trying to set up some image handling for a webpage I'm creating, but I can't get move_uploaded_file() to work properly... I keep getting these errors:
Warning: move_uploaded_file(/htdocs/PHP/Pictures/picture.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /opt/lampp/htdocs/PHP/useredit.php on line 17
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpY0KKxH' to '/htdocs/PHP/Pictures/picture.jpg' in /opt/lampp/htdocs/PHP/useredit.php on line 17
My code looks like this:
if(isset($_FILES['image_file']))
{
$img_tmp_name = $_FILES['image_file']['name'];
$img_dir = "/htdocs/PHP/Pictures/";
$img_name = $img_dir . $img_tmp_name;
if(move_uploaded_file($_FILES['image_file']['tmp_name'],$img_name))
{
list($width,$height,$type,$attr) = getimagesize($img_name);
switch($type)
{
case 1:
$ext = ".gif";
break;
case 2:
$ext = ".jpg";
break;
case 3:
$ext = ".png";
break;
default:
echo "Image format not accepted";
}
$query = "UPDATE profile_pic SET img_path=$img_name WHERE uid='$uid'";
$img_id = mysql_insert_id();
$new_img_name = $img_dir . $img_id . $ext;
rename($img_name, $new_img_name);
}
}
if(mysql_query($query)or die('Error: ' . mysql_error()))
{
header("Refresh:0; url='control.php'");
}
The folder PHP/Pictures exist. How do I fix this?
You've got some major security and logistical problems with this code:
a) You don't check if the upload succeeded and proceed as if it has. There's exactly ONE way for an upload to succeed, and far too many reasons for it to fail.
if ($_FILES['image_file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code {$_FILES['image_file']['error']}");
}
b) You're using the filename provided by the user in the path to save on your server. A malicious user can embed pathing data in that filename and specify any location on your server they want. e.g.
$_FILES['image_file']['name'] = '../../../../../../etc/passwd';
$img_dir should contain a path relative to you current file and not from root folder.
if your current directory contains upload_file.php (ur code) and a folder hierarchy like PHP/Pictures/
then $img_dir="/PHP/Pictures/";
I have this code to move my uploaded file to a specific directory:
if (isset($_FILES["image"]["name"])){
if (!is_dir('pf/' . $uid)) {
mkdir('pf/' . $uid);
$large_image_location = "pf/" . $uid;
}else {
$large_image_location = "pf/" . $uid;
}
chmod ($large_image_location, 0777);
move_uploaded_file("$userfile_tmp", "$large_image_location/$userfile_tmp");
}
However that gives the following error:
( ! ) Warning: move_uploaded_file(pf/BfyhieniKJGGqTNm/C:\wamp\tmp\phpF08A.tmp) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\wamp\www\mingle\upload_dp.php on line 26
Any help on how to sort this out would be greatly appreciated!
This is 90% of your woes:
move_uploaded_file("$userfile_tmp", "$large_image_location/$userfile_tmp");
You using the moved to path at the beginning of the upload path. Try:
move_uploaded_file("$userfile_tmp", "$large_image_location/".$_FILES['image']['name']);
That should work better.
The error itself is pretty clear, pf/BfyhieniKJGGqTNm/C:\wamp\tmp\phpF08A.tmp is not a valid filename.
Don't change the contents of $_FILES[n]['tmp_name'] (or $userfile_tmp for that matter), since it will always contain the full path to the uploaded file.