PHP copy() creates an empty image file - php

I have one image in a directory on my server, and want to copy it to another directory.
So I'm using
$post_picture = 'http://mysite.com/image.jpg';
copy($post_picture,
'images/pictures/post/thumb/' .
$info['filename'] .
'_thumb.' .
$info['extension']);
The issue is that in fact a file is created in my thumb directory, but that image is empty (0 x 0 pixels). I get no errors.
Any idea what is happening?
Permissions on all dirs are 755, both original and copy image have 644. The original show normally on a browser.
Thanks.

Do you have any form of hot-link protection that could alter what php receives?
Is allow_url_fopen allowed?

The $post_picture variable should probably have the file system path to the file, rather than the URL to the file.

Is allow_url_fopen set to true in your php.ini?
Sometimes that can produce this result if remote connections are being blocked.

$post_picture should be a local path i believe
copy( '/path/image.jpg', ... );

Please use this one...
<?php
$source = 'f-1/2.jpg';
$destination = 'f-2/2.jpg';
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
?>

Related

move_uploaded_file() is not working but file is uploaded?

So i'm currently trying to create a code which simply creates and publishes a file to my webroot, modifies and writes to that file, and then finally change the location of the file to another directory/folder using move_uploaded_file()
This is my code so far
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,'$dir/$title.txt');
fwrite($myfile, $_POST['textarea11']);
fclose($myfile);
The code doesn't work, i've tried echoing move_uploaded_file() and it returned nothing, however the file was uploaded but it's location just wasn't changed.
$dir is defined as $dir = __DIR__.'/../uploads/'; and $title is define as $title = $_POST['title'];
move_uploaded_file() can only be used if you are submitting a multipart form and you want to save the uploaded file.
What you probably need is this:
http://php.net/manual/en/function.rename.php
Change your given code as
$dir = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'uploads';
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,"$dir".DIRECTORY_SEPARATOR."$title.txt");
fwrite($myfile, $_POST['textarea11']);
In your code
move_uploaded_file($myfile,'$dir/$title.txt');
php variable $dir and $title value is not coming. and value of $dir is consisting '/' and you are adding one more to make full file path too.
Always use directory separator to run in all Operating System. some OS use '/' and some OS use '\'.

PHP fopen() with "wb" not working

Here is a snippet of what I'm trying to do:
$file = fopen($path, "wb");
fwrite($file, $data);
fclose($file);
Simple enough.
But when I open the created file, I see 0x0D inserted before 0x0A everywhere. I understand that this will happen if I open the file without binary mode.
But I've clearly specified I want binary mode. Maybe my brain isn't functioning right or something, so.. Anyone got a solution?
It turns out, for some weird reason, the problem was with my $path. My $path value was "temp".
It would generate the file named "temp" but would refuse to open it in binary mode. Giving the file an extension like "temp.bin" or "temp.tmp" allowed it to work in binary mode.
Problem solved for now but I'm still wondering why it works like this.
Seems the problem is with the $path. Please make sure you have given the correct file path.
If you are defining the $path with a dynamic file name, use / before the file name. For example, $var = "/var/www/html/projectFolder/folderFile/". "Filename.fileformat"
If you're working with URLs in a redirection context, then the root directory ('/') refers to your domain's root. The same goes for paths for linking files or images and for include and require directives.
You're making the classic mistake of confusing data with the representation of that data.
Let's say you have a text file. If you open it in Notepad, you'll see the following:
$str = "Hello world!";
echo bin2hex($str); // output: 48656c6c6f20776f726c6421
$file = fopen($path, "wb");
$data = bin2hex($data);
fwrite($file, $data);
fclose($file);

(Used to work!) Get Facebook Picture and Copy To Server

I have this code in PHP that used to work but now it isn't working.
I'm trying to grab the user's profile picture from Facebook, then copy and paste it into a directory on my server.
$url = 'http://graph.facebook.com/'.$fb_userID.'/picture?type=large';
$data = file_get_contents($url);
$fileName = 'users/avatars/'.$fb_username.$fb_userID.'.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);
Update: This code will put a jpg image into my directory with a size of 0 Kb so I'm pretty sure the content is never being grabbed correctly. Please help!
The solution was to adjust something in my php.ini file (or php5.ini) on my server.
I had to change this:
allow_url_fopen = Off
To on:
allow_url_fopen = on
Done.

PHP fwrite() not working

I'm writing a function in php, client side I have a canvas image which I use toDataUrl() along with a file name to save the image on the server. The here's the code:
<?php
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
$data = json_decode($imageData, true);
$file = $data["file"];
$image = $data["data"];
$filteredData=substr($image, strpos($image, ",")+1);
$unencodedData=base64_decode($filteredData);
$fp = fopen( 'image/' . $file , 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
?>
The thing is that this code works. And for two out of three of the pages I used it on it works fine. The problem is when I copy and pasted it a third time to implement it again, for some reason the file is made on the server except that no data get's written into the file. I don't think it's a problem client side because I write in a debug alert message in the javascript and a debug echo into the PHP and both are able to print out the data fine. I made this short debug file:
<?php
$fp = fopen('data.txt', 'wb');
if(is_writable('data.txt')){
echo "file is writable<br>";
}
if(fwrite($fp, 'test') == FALSE){
echo "failed to write data<br>";
}
fclose($fp);
?>
And the output is
file is writable
failed to write data
I've tried using chmod and setting everything, the folder, the text file before I write to it to 0777 and I still get the same result; the file is made but no data is written into it. Is there anything I'm missing or any other approaches that might help. I haven't found anything on google and am still baffled as to why the same code worked exactly as expected twice before suddenly stopping for no apparent reason.
Thanks in advance.
I know this is an old post, but I had a very similar problem and found a solution (for me at least)! I ran out of disk space on my server, so it could create a 0 byte file, but wouldn't write to it. After I cleared out some space (deleted a 13gb error.log file) everything started working again as expected.
If fopen works but fwrite mysteriously doesn't, check your disk space. 'df -h' is the command to check disk space on a linux server.
instead of $fp = fopen('data.txt', 'wb'); give $fp = fopen('data.txt', 'w'); and try
Changed "wb" to "w"
When you write $fp = fopen('data.txt', 'w'); for your domain website.com having root at /var/www/website/ and if the php file is located at /var/www/website/php/server/file/admin.php or something similar, it will actually create a file at /var/www/website/data.txt
Try giving absolute path or path relative to your domain root to create files like,
$fp = fopen('php/server/file/data.txt', 'w');
Try the find command to see if the file is created anywhere else in the folder directory by using the following in Ubuntu,
find /var/www/website/ -name 'data.txt'
I had this issue, probably can help you solve if you have similar issue.

Is there a way to use file_put_contents() and store the cached contents in a different directory?

I am able to put the file contents if I stay in the same directory level
ie: `file_put_contents('cache.txt', $result);
But how would I put the contents up a folder, or even two? Adding '../cache/cache.txt' does not seem to work.`
That would work fine assuming the user the web server runs as has write permission to that directory.
Try using the direct path of where you would like to put the cache content.
<?php
$file = 'people.txt';
$cacheFile = __FILE__ . '/cache/'. $file;
$content = 'Something';
file_put_contents($file, $content);
file_put_contents($cacheFile, $content);
?>
Assuming you have a folder called cache, with write permissions to it.

Categories