This test is on localhost/windows/php/apache/mysql
For example:
in root of Public folder:
$path = '/upload/images/accounts/12005/thumbnail_profile_eBCeBawXWP.jpg';
I want use it:
$img->save($path);
I get this error:
"Can't write image data to path
(/upload/images/accounts/12005/thumbnail_profile_eBCeBawXWP.jpg)"
I try to fix it:
$path = public_path($path);
$img->save($path);
So I get this error:
"Can't write image data to path
(D:\Server\data\htdocs\laravel\jordankala.com\public_html\ /upload/images/accounts/12005/thumbnail_profile_pH657T62fl.jpg)
probably, in real server (linux), I won't have this problem and the code words.
Now how can I manage this error? (I want it works in windows localhost and real linux server)
You can use this to save at your path
$file = $request->file('image_field_name');
$destinationPath = 'upload/images/accounts/12005/';
$uploadedFile = $file->move($destinationPath,$file->getClientOriginalName()); //move the file to given destination path
Related
I am trying to upload a file to public/attachments/foo.jpg in Laravel using storeAs(), it is working correctly in ubuntu but not in windows.
if($isValidated){
$newFileName = '';
foreach($files as $upload){
$fileName = preg_replace('/\s+/', '_', pathinfo($upload->getClientOriginalName())['filename']);
$newFileName = $fileName.'_'.$upload->uploadTime.'.'.$upload->getClientOriginalExtension();
$upload->storeAs('public/attachments', $newFileName);
}
}
This block of code successfully uploads a file in /public/attachments/foo.jpg
But when I try this in windows platform I get a error saying fopen ... failed to open stream : Invalid aruguments.
I have attached the screenshot of the error .
NOTE :
I have added symlink like so php artisan:storage link
Using Laravel 5.4
Problem is in the name of the file. It contains a colon :, which is not allowed on Windows.
The following code works perfect on my localhost but it shows the following errors on my live server
Warning: move_uploaded_file(.../uploads/76948893.jpeg): failed to open stream: No such file or directory
Warning: move_uploaded_file(): Unable to move '/tmp/phppxvRs8' to '.../uploads/76948893.jpeg'
What it does is simple, it takes the images on the array ["pictures"] which comes from a html form and save every image on the folder ".../uploads/" using a random numeric name as name of the file and keeping the original extension.
Any one knows how to make it work on my server?
//Image Uploader
$images=[];
$directory = '.../uploads/';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
$new_file_name = rand (10000000,99999999);
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
/* echo '<br>';
echo $directory.$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1);*/
if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key],
$directory
.$new_file_name
.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1))) {
array_push($images,$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1));
$images_validator=true;
}else{
//Error
}
}
}
There could be many reason for this, Check the following
You need WRITE Permission for the uploads directory. I assume your local machine runs windows & your hosting environment is linux
Like #Darren suggests, use absolute path. change the $directory to $directory = getcwd() . 'uploads/';
If this runs on your local machine but not on server there are 2 easy answers I can think off right off the back. 1. The folder doesnt exist on the server, 2. as someone comment you dont have permission to write/read to that folder on the server....I would check the server configuration to make sure your app pool or users have read/write permissions to the folder
I can't download file from ftp.
I want to download javascript file (3.js) from Root folder of FTP
$this->load->library('ftp');
$config['hostname'] = 'xxx.xx.x.x';
$config['port'] = 'xxx';
$config['username'] = 'ftpuser';
$config['password'] = '123456';
$config['debug'] = TRUE;
$this->ftp->connect($config);
$this->ftp->download("/3.js", "http://test.net/public_html/js/3.js");
But I have this Error
FTP download Unable to download the specified file. Please check your path
Then I will try this method
$files = $this->ftp->list_files('');
foreach($files as $file)
{
$this->ftp->download($file, "http://test.net/public_html/js/".basename($file));
}
But error will happend
Please help me!
$this->ftp->download("/3.js", "http://test.net/public_html/js/3.js");
should probably be
$this->ftp->download("/home/remoteacct/3.js", "/home/localacct/public_html/js/3.js", "ascii");
Your asking the ftp server to send you the file: '/3.js' which isn't a specific enough path. You are also asking codeigniter to store the file via http instead of via the filesystem. Add the full server path to the file you are downloading and change the web path to a server path.
Change 'remoteacct' and 'localacct' to match your specific directory information
I've been trying to make a system that can upload large files, originally i used HTTP but this had a number of problems with settings that needed to be changed. So i thought i'd give it a go with FTP.
Now i have a ftp connection in PHP and it works find, i can view folders and files as well as make directories, what i can't seem to figure out though is how to get hold of a local file and upload it.
I have been reading lots of information and tutorials such as the PHP manual and a tutorial i found on nettuts But i'm struggling to do it. The tutorial says you can upload a local file but i must be missing something.
Here is the upload method i'm using:
public function uploadFile ($fileFrom, $fileTo)
{
// *** Set the transfer mode
$asciiArray = array('txt', 'csv');
$extension = end(explode('.', $fileFrom));
if (in_array($extension, $asciiArray))
$mode = FTP_ASCII;
else
$mode = FTP_BINARY;
// *** Upload the file
$upload = ftp_put($this->connectionId, $fileTo, $fileFrom, $mode);
// *** Check upload status
if (!$upload) {
$this->logMessage('FTP upload has failed!');
return false;
} else {
$this->logMessage('Uploaded "' . $fileFrom . '" as "' . $fileTo);
return true;
}
}
When trying to upload a file i use this:
$fileFrom = 'c:\test_pic.jpg';
$fileTo = $dir . '/test_pic.jpg';
$ftpObj -> uploadFile($fileFrom, $fileTo);
I thought this would get the file from my machine that is stored in the c: and upload it to the destination but it fails (Don't know why). So i changed it a little, changed the $fileFrom = test_pic.jpg and up the picture in the same folder on the remote server. When i ran this code the script copied the file from the one location to the other.
So how would i go about getting the file from my local machine to be sent up to the server?
Thanks in advance.
Using this you would upload a file from your PHP server to your FTP server, what actually not seems to be your target.
Create an upload form which submits to this PHP file. Store this file temporarily on your server and then upload it to your FTP server from there.
If your try would actually work, this would be a major security issue, because a PHP file would have access to any files on my local machine.
I have a form, which uploads an image.
I try to get it by $_FILES:
$filename = $_FILES['screenshot']['name'];
$source = $_FILES['screenshot']['tmp_name']."/".$filename;
$target = GL_UPLOADPATH.$filename;
echo "TEST0";
if (move_uploaded_file($source, $target)) {
//connect to DB and so on, what I need
echo "TEST1";
}
So I get echoed TEST0 but don't get echoed TEST1.
If I echo every variable - it's normal. I see my $target - it's something like /tmp/phpoLqYdj/test2.jpg
So, I think PHP can't move_uploaded_file because it can't find /tmp/phpoLqYdj/test2.jpg
But where is /tmp/phpoLqYdj/? I am testing on localhost. My document root is /var/www/.
PHP has default settings in php.ini (upload_tmp_dir is commented in php.ini).
In my /tmp/ folder (in system) I don't have such folder like php***. In /var/tmp/ either.
(Ubuntu 10.10, LAMP was installed by "tasksel")
When you uploads files via PHP, it stores them in as a tmp file that's not named anything related to the filename. Appending $filename to $_FILES['screenshot']['tmp_name'] is the incorrect way to handle it... $_FILES['screenshot']['tmp_name'] IS the file.
Also, the tmp file is removed at the end of the request, so you'll never have a chance to actually see it in the file manager. Either you move it in the same request, or it's gone. It's all done in the name of security.
Anyway, just use this instead and you'll be good.
$filename = $_FILES['screenshot']['name'];
$source = $_FILES['screenshot']['tmp_name'];
$target = GL_UPLOADPATH.$filename;
echo "TEST0";
if (move_uploaded_file($source, $target)) {
//connect to DB and so on, what I need
echo "TEST1";
}
I had the exact same problem. What I did to fix it was change the permissions of folder that you are uploading to, to read and write for all.