imagejpeg(): Unable to open - php

PHP Warning: imagejpeg(): Unable to open '.Project/events/timepass.jpg' for writing: No such file or directory in ./Project/upload/thumbnal.php on line 35
code..
<?php
// open the directory
$pathToImages="./Project/upload/original/";
$dir = opendir($pathToImages);
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir )))
{
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg')
{
// echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$image_size=getimagesize( "{$pathToImages}{$fname}");
$image_width=$image_size[0];
$image_height=$image_size[1];
$new_size=($image_width+$image_height)/($image_width*($image_height/80));
$new_width=$image_width*$new_size;
$new_height=$image_height*$new_size;
$new_image=imagecreatetruecolor($new_width,$new_height);
$old_image=imagecreatefromjpeg("{$pathToImages}{$fname}");
imagecopyresized($new_image,$old_image,0,0,0,0,$new_width,$new_height,$image_width,$image_height);
$pathToThumbs="./Project/events/$fname";
imagejpeg($new_image,$pathToThumbs);
// save thumbnail into a file
}
}
// close the directory
closedir( $dir );
?>
I am getting this error when i transferred my data from localhost to live server FTP.I searched on google some have recommanded for changing attributes of directory to 777.i did tat bt no use same warning.Please tell where should i make changes to make these code work.

You transfer the new image to a remote server?
Where is the path to the server? Like "--IP TO SERVER--/Project/events/"
The problem is, the path you have wrote can not be found on your local machine.
UPDATE
To upload the image to the FTP server have a look at this example:
<?php
$file = 'somefile.txt';
$ftp_server = "ftp.example.com";
// Connection
$conn_id = ftp_connect($ftp_server);
// Login with user and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// File upload
// $remote_file is the filename on the server
// $file the filename on your local machine
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "success\n";
} else {
echo "error\n";
}
// Close connection
ftp_close($conn_id);
?>

In general when you have such problems, make sure that the path exists and the user with which you try to upload the image has the necessary privileges to that path.

Related

files uploaded by ftp_put are becoming shortcuts on the server [duplicate]

I have a bunch of movies that I'm trying to transfer from my CentOS server onto my Windows PC. But when I run them through this script they end up being corrupt. Is there something wrong with the script?
Thanks
$allFiles = glob("/var/www/html/ftp_pending/*");
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
foreach($allFiles as $singleFile)
{
// check if a file exist
$path = "/"; //the path where the file is located
$file = substr( $singleFile, strrpos( $singleFile, '/' )+1 );
$check_file_exist = $path.$file; //combine string for easy use
// Returns an array of filenames from the specified directory on success or
// FALSE on error.
$contents_on_server = ftp_nlist($conn_id, $path);
// Test if file is in the ftp_nlist array
if (in_array($check_file_exist, $contents_on_server))
{
echo "$file is already on FTP Server, no need to re-upload <br />";
}
else
{
$localfile = '/var/www/html/'.$file.'';
$remote_file = $file;
// upload a file
if (ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
{
echo "successfully uploaded $file\n";
}
else
{
echo "There was a problem while uploading $file\n";
}
};
}
// remember to always close your ftp connection
ftp_close($conn_id);
You're trying to upload something other than a text-based file while using
(ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
You should be using FTP_BINARY instead of FTP_ASCII since movies (and images) are binary files.

PHP FTP Upload function

i have this function in PHP:
function UploadFileToFTP($local_path, $remote_path, $file, $filename) {
global $settings;
$remote_path = 'public_html/'.$remote_path;
$ftp_server = $settings["IntegraFTP_H"];
$ftp_user_name = $settings["IntegraFTP_U"];
$ftp_user_pass = $settings["IntegraFTP_P"];
//first save the file locally
file_put_contents($local_path.$filename, $file);
//login
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
}
//change directory
ftp_chdir($conn_id, $remote_path);
$upload = ftp_put($conn_id, $filename, $local_path.$filename, FTP_BINARY);
// check upload status
if(!$upload) {
echo "FTP upload has failed!";
}
// close the FTP stream
ftp_close($conn_id);
}
i call it here:
UploadFileToFTP('p/website/uploaded_media/', 'media/', $_FILES["file"]["tmp_name"], $filename);
the selected file is being moved into the local directory and also being uploaded to FTP however the file is becoming corrupt because it is not being uploaded correctly.
how can i get the file uploading properly?
Depending on what kind of file you're moving you may need to switch from FTP_BINARY to FTP_ASCII
http://forums.devshed.com/ftp-help-113/ftp_ascii-ftp_binary-59975.html
When uploading a file to PHP it stores the uploaded file in a temporary location, the location is stored in $_FILES["file"]["tmp_name"].
You are then passing that value into your UploadToFTP function as the variable $file.
Then you try to save a copy of the uploaded file:
//first save the file locally
file_put_contents($local_path.$filename, $file);
What this will do is write the string contained within $file (i.e. the path of the temp file) to your new location - but you want to write the content of the file.
Instead of using file_put_contents use move_uploaded_file:
move_uploaded_file($file, $local_path.$filename);

php upload local file to ftp server

i understand that the ftp_put method uploads a file from the local server computer to the ftp server but i have problems using it where when i try to execute a simple script like this:
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
the operation is successfully done except for that the file uploaded to the my ftp server is always with zero byte size!
also i tried to enable passive mode but it still uploads an empty file.
Try enabling track_errors and access $php_errormsg
ini_set('track_errors', 1);
// put operation
// if error
var_dump($php_errormsg);
I had the same issue. When I change "FTP_ASCII" to "FTP_BINARY" it solved my problem and files uploaded as expected.
I had ran into this as well. Bit late but thought I'd post my solution:
$file_name = "localfile.txt";
Get content from your existing file
$content = file_get_contents('http://www.somewhere.com/'.$file_name);
...or make temp file content
$content = "This is content";
upload file
// connect
$conn_id = ftp_connect($host);
$login = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
// create
$tmp = fopen(tempnam(sys_get_temp_dir(), $file), "w+");
fwrite($tmp, $content);
rewind($tmp);
// upload
$upload = ftp_fput($conn_id, "serverfile.txt", $tmp, FTP_ASCII);
// close
ftp_close($conn_id);

Why would uploading a zip file via ftp_fput return true but not upload the file?

The following command returns true and uploads the text XML file to the FTP server:
if (ftp_put($this->ftpConnectionId, $this->remoteXmlFileName, $this->localXmlFileName, FTP_ASCII)) {
However, when I try to upload a .zip file intead of a text XML file, it still returns true but does not upload the file:
if (ftp_put($this->ftpConnectionId, $this->remoteXmlFileName, $this->localXmlFileName, FTP_BINARY)) {
I found that if I simply rename the zip file to ".xml", it WILL upload the file but the .zip file is corrupted.
But if I rename the zip file to ".zip.xml" it again returns true but does not upload the file.
What could be the reasons for this odd behavior?
Additional Info:
A zip file can be uploaded via FileZilla no problem with the same account.
I also am specifing:
ftp_pasv($this->ftpConnectionId, true);
A zipfile is a binary file. That's probably why uploading it as .xml corrupts the file. Try specifying FTP_BINARY instead of FTP_ASCII. FTP_BINARY will work for ascii files too, but not vice versa, so you can better always use FTP_BINARY than always FTP_ASCII.
The ftp server may reject the file for many reasons, so it may allow the upload at first, but then not save the file. The ascii/binary problem may be one, but also some file extensions may be blacklisted, or the file could be too big. The latter is unlikely, though, since uploading the zipfile with a different extensions worked for you.
I think the ftp server actively ignores zip files.
This is because zip file contains Files and may be is size is greater than your XMl
We have used this code to upload entire directory over ftp
Try this code. This will work for your ftp
//Start ftp upload code
$ftp_user_name =$_SESSION['upload']['username'];
$ftp_user_pass = $_SESSION['upload']['password'];
$ftp_server = $_SESSION['upload']['host'];
$sourcepath = $_SESSION['upload']['source'];
$dest_folder = $_SESSION['upload']['dest_folder'];
$conn_id = #ftp_connect($ftp_server,21) or die("Couldn't connect to $ftp_server");
if (#ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 70000000000000000); // Set the network timeout to 10 seconds
ftp_copyAll($conn_id, $sourcepath, $dest_folder);
}
function ftp_copyAll($conn_id, $src_dir, $dst_dir) {
if(is_dir($dst_dir)){
return "Dir $dst_dir Already exists";
} else {
$d = dir($src_dir);
ftp_mkdir($conn_id, $dst_dir); //echo "creat dir $dst_dir";
while($file = $d->read()) { // do this for each file in the directory
if ($file != "." && $file != "..") { // to prevent an infinite loop
if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
$src_dir_path=$src_dir."/".$file;
$dst_dir_path=$dst_dir."/".$file;
ftp_copyAll($conn_id, $src_dir_path, $dst_dir_path); // recursive part
} else {
$upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
//echo "creat files::: ".$dst_dir."/".$file ."";
echo " ";
}
}
ob_flush() ;
flush();
usleep(90000);
//sleep(1);
}
$d->close();
}
return true;
}

Moving an uploaded file onto a remote server

I am trying to move an uploaded file onto a remote server, this isn't working;
move_uploaded_file($tmp_name, "uploads/$code1/$code.$fileex");
$ftp_server = "IP";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$file = $tmp_name;
$remote_file = "/public_html/test/uploads/";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
I get this erorr;
Warning: ftp_put() [function.ftp-put]: Can't open that file: Is a directory in /home/file/public_html/uploaded.php on line 52
Your $remote_file variable is pointing to a directory when it should point to a file. Try changing $remote_file to $remote_file = "/public_html/test/uploads/".$file;
You should probably wrap the portion that uploads the file in an if statement that checks to see if you are actually connected properly to the FTP
Also, when uploading a file, you need file 1 and file 2. Right now you've supplied file 2 and a directory.
http://php.net/manual/en/function.ftp-put.php
The file you are trying to move to is the directory "/public_html/test/uploads/", you need to append the filename and extension onto the directory.
Add the following line at the end of the /etc/vsftpd.conf file
Add pasv_promiscuous=YES it

Categories