I am unable to move file into desired folder. I want to save image into uploaded folder. In mysql i have it in blob type.
this is my code
$target_Path = "uploaded/";
$target_Path = $target_Path.basename( $_FILES['image']['name'] );
move_uploaded_file( $_FILES['image']['tmp_name'], $target_Path );
mysqli_query($con,"UPDATE info SET photo='$target_Path' WHERE user_id='$id'");
In mysql it is showing that something has been saved but it not opening and the file is not moving into uploaded folder. i am doing this in my localhost. Please help.
Try wrapping your move_uploaded_file with an IF statement, and throw an exception if the file fails to transfer:
if (move_uploaded_file($_FILES['image']['tmp_name'], $target_Path ) === false) {
throw new Exception([text]);
}
Alternatively, to test whether the destination is writeable, you could try opening a file there and writing to it:
if (($fp = fopen($target_path, 'w')) === false) {
thrown new Exception("Failed to open file $target_path for writing");
}
fwrite($fp, file_get_contents($_FILES['image']['tmp_name']));
fclose($fp);
Chances are the problem is one of file permissions or ownership, which you'll need to fix at the command line with chmod or chown.
Related
So, I want to create a system where user uploads in a zip file the files of a 3d model and the model can be shown, stored, etc
So, I got the file, I place it into a folder, permanently, And unzip it into another temp folder, just to see if it is a 3d model.
I tried like this:
$target_dir = "upload/";
$targetfilename = rand().$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $target_dir.$targetfilename);
//unzip the file into temp folder
$tmp_dir = $target_dir.rand();
mkdir($tmp_dir);
chmod($tmp_dir, 0777);
//chmod($targetfilename, 0777); //this not working, maybe isn't the right way
$zip = new ZipArchive;
$res = $zip->open($targetfilename);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($tmp_dir);
$zip->close();
echo 'SUCCESS';
} else {
echo 'ERROR';
}
I do not get any errors, but the zip can't be unzipped. Any idea? How can I resolve this?
Isn't $targetfilename is in $target_dir folder?
If so, changing
$res = $zip->open($targetfilename); to
$res = $zip->open($target_dir.$targetfilename); might solve your problem.
I have a problem with moving uploaded files.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
$sql = "INSERT INTO items (name , description,`price`, `country`, `release`, `condition`, `image`)
VALUES ('$name','$description','$price', '$country', '$date', '$condition', '$targetFileForItem')" ;
?>
the variable $targetFileForItem works currect, and inserts into my db very well, but the file don't move into $target_filevar's folder, which is uploads. As you see I use move_uploaded_file() function, but i's not working. Any suggestions?
Write this to debug
ini_set('display_errors',1);
error_reporting(E_ALL);
If your code is ok then check file permissions you can use this
if (is_dir($target_file ) && is_writable($target_file )) {
// do upload logic here
} else {
echo 'Upload directory is not writable, or does not exist.';
}
is_writable Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable
for more info read this http://php.net/manual/en/function.is-writable.php
Check your permission for upload folder it must be 775. If you are using FTP than right click on folder and change File permission of that folder to 755.
If it's localhost then it must be a path or folder name issue.
And make your code like this so you can get find out errors also.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
// if folder not exists than it will make folder.
if(!file_exists($target_file))
{
mkdir($target_file, 0777, true);
}
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_file))
{
echo "file successfully uploaded";
}
else
{
echo "error in file upload";
}
?>
after uploading prestashop from my localhost to the server , a problem showed up
when trying to upload product image i got this error "Server file size is different from local file size"
after searching in prestashop files i found that is uploader class is responsible for the upload process.
public function upload($file, $dest = null)
{
if ($this->validate($file))
{
if (isset($dest) && is_dir($dest))
$file_path = $dest;
else
$file_path = $this->getFilePath(isset($dest) ? $dest : $file['name']);
if ($file['tmp_name'] && is_uploaded_file($file['tmp_name'] ))
move_uploaded_file($file['tmp_name'] , $file_path);
else
// Non-multipart uploads (PUT method support)
file_put_contents($file_path, fopen('php://input', 'r'));
$file_size = $this->_getFileSize($file_path, true);
if ($file_size === $file['size'])
{
$file['save_path'] = $file_path;
}
else
{
$file['size'] = $file_size;
unlink($file_path);
$file['error'] = Tools::displayError('Server file size is different from local file size');
}
}
return $file;
}
when commenting if statement witch responsible of comparing file size and tring to upload an image i got this error
"An error occurred while copying image, the file does not exist anymore."
i changed img folder permissions to 777 still same problem ?
Its a permission issue. I resolved it by giving complete access (777) to cache and theme folders.
If that doesn't help then try giving complete access to all the files. Be sure to change back the permission to 755 for folders and 644 for files for security reasons.
please change " if ($file_size === $file['size']) " to " if ($file_size = $file['size']) "
This method worked for me.
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.
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;
}