i am getting an error in uploading image into folder. there is an error occured during uploading that image.
my Controller Code is (cakeplus is my root folder ex: htpp://localhost/cakeplus) :
$directory = "http://".$_SERVER['HTTP_HOST'].'/cakeplus/pics';
if(!is_dir($directory)) {
mkdir($directory,0777);
}
$files_array = $this->data['Photo']['path'];
//pr($files_array); die;
if(isset($files_array['name']) && $files_array['name']!='') {
$filetype = $files_array['type'];
$filesize = $files_array['size'];
$filename = $files_array['name'];
$filetmpname = $files_array['tmp_name'];
$file_type = explode('.',$filename);
$ext_name = array_reverse($file_type);
$final_file_title = $ext_name[1];
$file_name = Inflector::slug( str_replace( ".".$ext_name[0], "" , $filename ). '_' .time() ,'-' );
$newFileName = $file_name.'.'.$ext_name[0];
move_uploaded_file($filetmpname, $directory.'/'.$newFileName);
$requirementuploadData['Photo']['path'] = $file_name;
$this->Photo->create();
$this->Photo->save($requirementuploadData,false);
}
Error(s)(Warnings) :
Warning (2): move_uploaded_file(http://localhost/cakeplus/pics/wallpaper-1433586197.png): failed to open stream: HTTP wrapper does not support writeable connections [APP\Controller\PhotosController.php, line 31]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpA80D.tmp' to 'http://localhost/cakeplus/pics/wallpaper-1433586197.png' [APP\Controller\Photos
Look into the CakePHP Upload plugin - it will abstract away much of the work that goes into dealing with file and image uploads.
The error you are seeing is because you cannot use move_uploaded_file() to transfer from a file path (C:\xampp\tmp\phpA80D.tmp) to an HTTP URL (http://localhost/cakeplus/pics/wallpaper-1433586197.png).
If you don't want to use the Upload plugin, and would prefer to keep working with what you already have, I would start by changing the $directory path. Something like this might be more appropriate:
$directory = WWW_ROOT . 'pics';
This will contain the path to your ./yourapp/webroot/pics directory, which is also the location of http://yourapp.com/pics.
Check out the documentation for more predefined paths.
may be folder dont have permission to write an image.
you should to use cakephp upload component.
$this->Upload->upload($file,$destination,$name);
Related
I don't know if this has been asked before, please pardon in advance.
I created an image compression web app using the tinyPNG API, and it works as intended - that is users can upload an image file and it is compressed and saved in the server.
But then I create a download functionality for the user to download/save the compressed image.
On clicking the download link it throws up an error:
can’t find the file at http://localhost/var/www/html/imgtest/uploads/67403938_10219921906785196_7063388762713096192_n_1574687362.jpg.
Even though the compressed image is in the server.
Please help!!!
MY CODE:
require_once("vendor/autoload.php");
//makes PHP execute faster
set_time_limit(0);
//API key
\Tinify\setKey("API_KEY_HERE");
//Main code
if (isset($_POST['submit'])) {
//supported image formats.
$supported_image = array('image/jpg', 'image/jpeg', 'image/png');
foreach($_FILES['images']['name'] as $key=>$val){
$file_name = $_FILES['images']['name'][$key];
// get file extension
$ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
// get filename without extension
$filenamewithoutextension = pathinfo($file_name, PATHINFO_FILENAME);
if (in_array($_FILES['images']['type'][$key], $supported_image)) {
if (!file_exists(getcwd(). '/uploads')) {
$oldmask = umask(0);
mkdir(getcwd(). '/uploads', 0777, true);
umask($oldmask);
}
$filename_to_store = $filenamewithoutextension. '_' .time(). '.' .$ext;
move_uploaded_file($_FILES['images']['tmp_name'][$key], getcwd(). '/uploads/' .$filename_to_store);
$compress_file = getcwd(). '/uploads/' .$filename_to_store;
// optimize image using TinyPNG
try {
$source = \Tinify\fromFile(getcwd(). '/uploads/' .$filename_to_store);
$source->toFile(getcwd(). '/uploads/' .$filename_to_store);
//The code to show a modal for downloading the converted file.
} catch(Exception $e) {
echo $e->getMessage();
exit;
}
}
}
echo "<a href=".$compress_file." download>Download</a>";
}
?>```
The problem in your code is, that you create the link with $compress_file that contains absolute path to the file.
/var/www/html/imgtest/uploads/67403938_10219921906785196_7063388762713096192_n_1574687362.jpg
So when the link is clickend and the url is resolved in browser the result is
http://localhost/var/www/html/imgtest/uploads/67403938_10219921906785196_7063388762713096192_n_1574687362.jpg.
So if the localhost domain is pointed to /var/www/html/imgtest/ than the server is looking for the file in this path which doesn't exist:
/var/www/html/imgtest/var/www/html/imgtest/uploads/...
When you are generating the path to the compressed file you should put it's url into other variable where you wouldn't prepend the current directory.
$compress_file_url = '/uploads/' .$filename_to_store;
$compress_file = getcwd(). $compress_file_url;
Then you should use the $compress_file_url instead of $compress_file when creating a link
echo "<a href=\"$compress_file_url\" download>Download</a>";
This should work assuming your localhost domain is pointed to /var/www/html/imgtest. If your localhost domain is pointed to /var/www/html you might need to prepend $compress_file_url with /imgtest before it's used to create a link.
$compress_file_url = '/uploads/' .$filename_to_store;
$compress_file = getcwd(). $compress_file_url;
$compress_file_url = '/imgtest' . $compress_file_url;
I want to upload image file to server. And I have failed to open stream: HTTP wrapper does not support writeable connections" error in "move_uploaded_file"
function. How can I fix it?
$image_src = $_FILES['ex_image']['tmp_name'];
$src = asset('assets/images/excavator/'.$title.'.png');
move_uploaded_file($image_src,$src);
You need to use base_path() method. This method returns the fully qualified path to the project root:
if ($request->hasFile('ex_image')) {
$destinationPath = base_path().'/assets/images/excavator/';
$file = $request->ex_image;
$fileName = $file->getClientOriginalName();
$file->move($destinationPath, $fileName);
}
and if you want to return the public directory then use:
$destinationPath = public_path().'/assets/images/excavator/';
This gives a Warning Message: filesize(): stat failed for http://localhost/wft/uploads/4_Sat_Sep_10_2016_16_18_52.pdf
$file_path = base_url().'uploads/4_Sat_Sep_10_2016_16_18_52.pdf';
$size = filesize($file_path);
how to find file size of pdf.
You could do this without using the URI, instead using the path to the File.
<?php
// CHANGE __DIR__ TO FIT WITH THE PATH TO THE ROOT DIRECTORY HOLDING THE PDF FILE
$file_path = __DIR__ . '/uploads/4_Sat_Sep_10_2016_16_18_52.pdf';
$size = filesize($file_path);
PHP filesize() function give the file size.
$path = "uploads/your_file";
$size = filesize($path);
NB: Path should be a relative path.
I am using uploadifive to upload images - however I want to change the name of the images to "newimage" I am not sure where or how to do this with what is provided - this is the last modification I need to deploy.
You're going to need to do the name change in the PHP script that handles the uploading (I'm assuming your using PHP since that's uplidfy's standard). It can be a bit tricky because you have to separate the incoming file name from it's extension. This should be enough to get you started.
$theFile = $_FILES['file_upl']['name'];
$tempFile = $_FILES['file_upl']['tmp_name'];
$newName = "newname";
$saveDir = $_SERVER['DOCUMENT_ROOT']."/images/";
//Function returns the extension from the '.' on
function get_ext($from_file) {
$ext = strtolower(strrchr($from_file,'.'));
return $ext;
}
//This will rename the file to $newname + the extension
$theFile = $newname.get_ext($theFile);
//Save the file
move_uploaded_file($tempFile,$saveDir.$theFile);
$file = 'uploaded_img.jpg';
$remote_file = 'Saved.txt';
ftp_put($conn_id, $remote_file, $file, FTP_ASCII)
just change the name of the remote file
Hello I am usign the below code to zip a package on upload:
$nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = './CopyrightFiles/';
$zip = new ZipArchive();
$fileconpress = $download_folder.$nameFile.".zip";
$conpress = $zip->open($fileconpress, ZIPARCHIVE::CREATE);
if ($conpress === true)
{
$zip->addFile($tmpName);
$zip->close();
echo $fileconpress."<br/>";
echo "yess !! Success!!!! ";
}
else echo " Oh No! Error";
It seems to work ok.
But there are two issues.
First issue it saves the file also with the original extension, something like : image.JPG.zip
Also when I then move the zip package to my local computer (Mac) and I open the ZIP inside I can find only a tmp folder with a binary file inside and NOT the image or the file that should be there!
What the hell is going on?
Please advise
Thank you
That's because your "binary" file is just the temporary name that PHP used to temporarily stored the uploaded file as. That's why it's "tmp_name" in the $_FILES array.
Try this:
$zip->addFile($tmpName, $nameFile);
The second parameter lets you specify what the file's name should be within the zip.
To prevent 'file.jpg.zip', try:
$fileconpress = $download_folder . pathinfo($_FILES['file']['name'], PATHINFO_FILENAME) . ".zip";
Issue #1:
You need to manually remove the extension:
$filename = explode('.', $_FILES['file']['name']);
$filename = $filename[0];
Issue #2:
$tmpName does not contain the filename of the file. You need to pass the $localname parameter in addFile(). See http://php.net/manual/en/function.ziparchive-addfile.php.