I have wrote a PHP file which will save a JPEG file in the server and part of the code is listed as follow:
//create folder if folder not exist
if (!is_dir($save_path)){
$old = umask(0);
$flag = #mkdir($save_path,0777);
umask($old);
if(isset($flag)){
$string = 'Folder Create Success!'."\n";
}else{
$string= 'Folder Create Fail!'."\n";
}
echo $string;
}else{
echo "Folder exist!!!!";
}
//write the content to the server
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: image/jpg; charset=utf-8');
if(!$file = fopen($path, 'wb')){
echo 'Image upload Fail!'."\n";
return;
}
else
{
fwrite($file, $binary);
fclose($file);
}
The problem is when I run the code, if the folder does not exist, it create the folder only but the content can't save in the folder. The error message is :
[Thu Jul 05 16:59:06 2012] [error] [client 10.95.61.220] PHP Warning: fopen(/mnt/csis/upload/newphoto/others/12346_test/12346_test_2012-07-05_others_abc.jpg): failed to open stream: Permission denied in /var/www/html/upload_image.php on line 57
However, if I run the code again, since the folder was created in the past, it work properly. The content can save in the folder......
Anything I get wrong? I try to find the answer on the web but still can't solve the problem.
Anyone can help, many thanks!
I would try changing the creation of the folder to use the recursive flag:
$flag = #mkdir($save_path . "/" . $file,0777,true);
Related
I am new to PHP, and adapting a script from http://www.techrepublic.com/article/create-a-dynamic-photo-gallery-with-php-in-three-steps/ which generates a table of images in a directory along with some accompanying EXIF data. The only problem is that the code has not display the EXIF data. This happens with even the original source code. My best guess of what is happening is that something in the original source code is old and outdated, and no longer supported by modern PHP. I have made sure that my server has EXIF enabled.
Here's the code:
<table>
<?php
// define directory path
$dir = "path/to/directory";
// iterate through files
// look for JPEGs
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (preg_match("/.jpg/", $file)) {
// read EXIF headers
$exif = exif_read_data($file, 0, true);
// get image
echo "<tr><td rowspan='3'><img src='$dir/$file'></td>";
// get file name
echo "<th>Title</th><td>" . $exif['FILE']['FileName'] . "</td></tr>";
// get timestamp
echo "<tr><th>Year</th><td>" . $exif['IFD0']['DateTime'] . "</td></tr>";
// get image dimensions
echo "<tr><th>Description</th><td>" . $exif['IFD0']['Comments'] . "</td></tr>";
}
}
closedir($dh);
}
}
?>
</table>
EDIT: I also get the following error logs:
20160815T185355: benxd.me/art/gallery.php
PHP Warning: exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gallery.php on line 21
PHP Warning: exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gallery.php on line 21
PHP Warning: exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gallery.php on line 21
PHP Warning: exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gal
In my code line 21 is $exif = exif_read_data($file, 0, true);
Try explicitly adding the full path and the list of sections:
$exif = exif_read_data($dir . $file, "FILE,COMPUTED,ANY_TAG,IFD0,THUMBNAIL,COMMENT,EXIF", true);
Source: http://php.net/manual/en/function.exif-read-data.php
I spent weeks with this problem, so in the end I decided to create a library to extract the metadata (Exif, XMP, GPS...) from a PNG in PHP, 100% native, I hope it helps. :) PNGMetadata
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 ?
Hello so i have a simple upload system in php and i want to upload my files to ftp server but when i try to it doesnt work i get these two errors:
Warning: move_uploaded_file(/userfiles/grega): failed to open stream: No such file or directory in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
Warning: move_uploaded_file(): Unable to move '/tmp/phpVtApVM' to '/userfiles/grega' in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
and there is folder userfiles/grega on the ftp server please help me out
the code:
<?php
require_once 'core/init.php';
if($_POST[submit]) {
$name = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
if($size <= 5000000){
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
$uploads_dir = '/userfiles';
$username = ($user->data()->username);
move_uploaded_file($temp,"$uploads_dir/$username");
Session::flash('home', '<h3>Datoteka je bila naložena!</h3>');
Redirect::to('mojprofil.php');
} else{
echo "Napaka!";
}
} else {
header("Location: mojprofil.php");
}
?>
You say this:
there is folder userfiles/grega
But the error says this:
move_uploaded_file(/userfiles/grega)
Those are two very (even if subtly) different paths. Note also where you define the path in your code:
$uploads_dir = '/userfiles';
The code is looking for a folder called userfiles in the root of the entire file system, not just in the website. Perhaps you meant to do this?:
$uploads_dir = 'userfiles';
I am attempting to transfer a zip file via ftp from one server to mine, so that I can use the data in the file to update my database table. Here is my ftp.php file:
<?php
header('Content-type: text/html; charset=utf-8');
$date = "2013-05-21-11-19-40";
$ftp_server="ftp.server.com";
$ftp_user_name="BookCellar";
$ftp_user_pass="*****";
$file = "/reports/other/compreport_abebooks_" .$date. ".zip";//tobe uploaded
$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip";
?>
and my ftpUpload.php file is:
<?php
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
header('Content-Type: application/zip');
require_once('ftp.php');
// set up basic connection
$conn_id = ftp_ssl_connect($ftp_server);//ftp_connect
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// check connection
if ((! $conn_id ) || (! $login_result )) {
echo "FTP connection has failed!" ;
exit;
} else {
echo "Connected for user $ftp_user_name" ;
}
ftp_chdir($conn_id, '/home/bookcell/bookcellaronline.com/html/testbcos/accounting/');
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
echo $php_errormsg;
// close the connection
ftp_close($conn_id);
?>
When I run these I get the error messge:
[<a href='function.ftp-put'>function.ftp-put</a>]: failed to open stream: No such file or directory in /chroot/home/bookcell/bookcellaronline.com/html/testbcos/accounting/ftpUpload.php on line 25
Line 25 is:
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
I have researched a bunch of other posts on SO and have not found a solution. My connection works, but I can't get the file to transfer.
How do I (if even possible) get this to transfer to my server?
EDIT: Am I missing the fact that I need to connect not only to their server($file) AND my server ($remote_file)????
You can't put a path to the destination file
$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip";
e.g. - this doesn't work
ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);
you have to put
<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
A FTP server hides his absolute path /home/bookcell/bookcellaronline.com/html/
All folders must be relative to the root
ftp_chdir($conn_id, '/testbcos/accounting/');
test the result of ftp_chdir ! Are your in the right directory ?
echo ftp_pwd($conn_id);
Try to connect to the FTP server via a browser.
ftp://BookCellar#ftp.server.com
What you get is / the root. Folders and files that you see in the browser, are below the root directory.
Update : A working out of the box example.
the $password = line must be replaced with Download pass
ftp.php
<?php
$password = "????";
$resource = ftp_connect('ftp.strato.com');
$login = ftp_login($resource, 'ftp_mx_all#moskito-x.de', $password);
$list = ftp_rawlist($resource, '/');
print_r($list);
?>
You will get with print_r
Array ( [0] => drwxr-xr-x 2 ftp ftp 4096 May 23 20:15 aFolder [1] => -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt )
we can see there is a folder aFolder and a file tutorial.txt.
We are interest what files are in the folder aFolder ?
So replace the $list = with
$list = ftp_rawlist($resource, '/aFolder');
and run the php script again. The output :
Array ( [0] => drwxr-xr-x 3 ftp ftp 4096 May 23 19:24 .. [1] => -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt [2] => -rw-r--r-- 1 ftp ftp 271 May 23 21:16 tutorial.zip )
Now we want to download aFolder/tutorial.txt .
Add following below print_r($list); .
echo "<br />\n";
$local_file = "tmp.txt" ;
$file = ftp_get($resource, $local_file, '/aFolder/tutorial.txt',FTP_ASCII);
if ($file) {
echo "$local_file has been successfully written\n";
} else {
echo "An error has occurred\n";
}
The Output :
The folder where the php script is has changed.
now there is a new file tmp.txt !
If you bring this little script to run. We can go further.
From our chat :
Your server does not allow a ftp call to a url.
look allow_url_fopen = ON
echo ini_get('allow_url_fopen');
if (!ini_get('allow_url_fopen')) {
ini_set('allow_url_fopen', 1);
}
echo ini_get('allow_url_fopen');
Then try it again.
set_time_limit(0);
exec("wget --continue http://example.com/site.zip");
exit;
In the first excerpt change the remote tile to be
`$remote_file = "compreport_abebooks_" .$date. ".zip";`
prior to the put you are changing into the directory.
Also note that the directory that you reference in the ftp_chdir call differs from the one in the $remote_file referenced at the top.
/home/bookcell/bookcellaronline.com/html/testbcos/accounting/
vs
/chroot/home/bookcellaronline.com/html/testbcos/accounting/
I use ziparchive to zip my files in linux, and can't open in windows
default zip uploader, I suspect it is caused by the file path during addfile
e.g.
$zip-addFile(‘/home/userName/public_html/gallery/license.txt’, ‘gallery/license.txt’);
Suggestion from links below mention that I coudl remove the local path(as this can't be understood by windows), which becomes becomes
$zip-addFile(‘/home/userName/public_html/gallery/license.txt’, ‘license.txt’);
http://www.jacobbates.com/blog/2012/04/24/corrupt-zip-files-in-windows-from-phps-ziparchive/
PHP ZipArchive Corrupt in Windows
But I need to maintain the directory structure, how should I address this problem?
maybe first add the target directory with addEmptyDir see the code below this create different files:
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$zip = new ZipArchive;
if ($zip->open('tmp/test.zip',ZipArchive::CREATE) === TRUE) {
$zip->addFile('data.php', 'data/data.php');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
$zip = new ZipArchive;
if ($zip->open('tmp/testdata.zip',ZipArchive::CREATE) === TRUE) {
if($zip->addEmptyDir('data')) {
echo 'Created a new root directory';
} else {
echo 'Could not create the directory';
}
$zip->addFile('data.php', 'data/data.php');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
The files got different file sizes:
-rw-r--r-- 1 www-data www-data 633 Apr 29 12:10 testdata.zip
-rw-r--r-- 1 www-data www-data 545 Apr 29 12:10 test.zip
unzip test.zip no empty dir added:
unzip test.zip
Archive: test.zip
inflating: data/data.php
unzip testdata.zip with an empty dir added:
Archive: testdata.zip
creating: data/
inflating: data/data.php
does creating: data/ first
Had the same problem with a structure similar to yours on a Drupal context. I solved this by setting the Content-disposition filename with my uri
file_transfer($archive_uri, array(
'Content-Disposition' => 'attachment; filename="' . $archive_uri . '"',
'Content-Length' => filesize($archive_uri))
);