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
Related
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.
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 ?
There is no solution, as i have searched a lot on moving my uploaded image into directory here is my php code :
<?php
//Profile Image upload script
if (isset($_FILES['profilepic'])) {
if (((#$_FILES["profilepic"]["type"]=="image/jpeg") || (#$_FILES["profilepic"]["type"]=="image/png") || (#$_FILES["profilepic"]["type"]=="image/gif"))&&(#$_FILES["profilepic"]["size"] < 1048576)) //1 Megabyte
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
$dir = "userdata/profile_pics/$rand_dir_name";
mkdir($dir);
move_uploaded_file($_FILES["profilepic"]["tmp_name"],"userdata/profile_pics/$rand_dir_name".$_FILES["profilepic"]["name"]);
$profile_pic_name = $_FILES["profilepic"]["tmp_name"];
echo $profile_pic_name;
$profile_pic_query = mysqli_query($conn,"UPDATE users2 SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$user'");
}
else
{
$msg5 = "Invailid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
}
}
?>
but when I try to upload image,it creates the random folder in userdata/profile_pics but it doesn't move the file into random folder directory i have also created a custom php.ini file with file_uploads = On, but i recieve the following warning when i submit the form :-
Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in /home/rahulkapoor90/public_html/note.php on line 26
Warning: move_uploaded_file(): Unable to move '/tmp/phpO592dd' to 'userdata/profile_pics/zAGC9wOhVyoe3R5' in /home/rahulkapoor90/public_html/note.php on line 26
/tmp/phpO592dd
For anyone else who comes across this error, in my case it was a permissions issue. I re-factored some legacy code and checked the wrong directory for permissions and instead kept getting this misleading error message.
Wasted few hours to get the correct cause. You can try 2 things
Verify your destination folder
`$destdir="YOUR-COMPLETE-DESTINATION-FOLDER/uploads/";
if(is_dir($destdir) && is_writable($destdir))
{echo "<strong>UPLOAD DIRECTORY EXIST & WRITABLE</strong>";}
else
{echo "<strong>ISSUE WITH UPLOAD DIRECTORY</strong>";}`
Confirm 2nd parameter of move_uploaded_file is a file name NOT A DIRECTORY NAME
In my case this turned to be the issue
if(move_uploaded_file($tempname,"$destdir/".$_FILES['profilepic']['name']"))
{echo 'FILE UPLOADED SUCCESSFULLY';}
else
{echo "FAILED TO UPLOAD";}
The site I am working on- http://24.236.131.128/
This is a site using the snapchat API. I can log into my snapchat account and SHOULD see the snaps I have that are unopened.
I can log in just fine, and view my friends or list of snaps. I can NOT, however, get the image to display or save to the path.
I get a Warning:
file_put_contents(media/download/from_lolnope47_to_lolnope47_id_959644398384684466r.jpg): failed to open stream: Permission denied in C:\inetpub\wwwroot\index.php on line 114 View pic...
the C:\inetpub\wwwroot\media\download\ folder has full permissions set to the IUSR account for IIS, not too sure if that's the problem?
Any suggestions?
EDIT:
Here is the function to download the snap:
if ($id == 'download')
{
echo '<title>Download picture</title>';
$snapchat = new Snapchat($_SESSION['name'],$_SESSION['pass']);
$snapid = $_GET['snapid'];
$sender = $_GET['sender'];
$recipient = $_GET['recipient'];
$data = $snapchat->getMedia($snapid);
$prePath = 'media/download/from_'.$sender.'_to_'.$recipient.'_id_'.$snapid.'.jpg';
if (file_exists($prePath))
{
$finalPath = 'media/download/from_'.$sender.'_to_'.$recipient.'_id_'.$snapid.rand(0,100).'.jpg';
}
else
{
$finalPath = 'media/download/from_'.$sender.'_to_'.$recipient.'_id_'.$snapid.'.jpg';
}
//echo "<img src='$data'></img>";
file_put_contents($finalPath, $data);
echo 'View pic...';
}
Try saving the image to the same folder where your php script is, you also need to set permissions all to the same user that php.exe is running, in last case scenario set permissions all to user everyone ( this is dangerous!).
Try using the full path C:/inetpub/wwwroot/media/download/... instead of the relative path media/download
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.