Given this one a few hours research and thought but I'm not getting anywhere.
I have managed to get an image file to upload through AJAX using a FormData object. When the file reaches the php code I am able to access its info such as 'name' and 'type' by using:
$_FILES['newFile']['name'];
$_FILES['newFile']['type'];
And so on, which means that the file must be uploading as intended, I just can't seem to save it to a file from there.
I have tried:
$file = file_get_contents($_FILES["newFile"]['tmp_name']);
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);
But then imagejpeg gives me the error "Expects paramater1 to be resource, string given."
So I tried:
$file = imagecreatefromstring(file_get_contents($_FILES["newFile"]['tmp_name']));
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);
I now receive the error: /"imagejpeg('/img/uploads/image.jpg'): Failed to open stream. No such file or directory."
Can someone explain how I get the image from the array to a file on disk?
You have to use the move_uploaded_file function like this:
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $pathfilename)
Use php function move_uploaded_file("$_FILES['newFile']['tmp_name']","Location_where_you_want_to_save");
Figured it out! Obviously "/img/uploads/" was the issue. PHP doesn't recognize a forward slash as the root directory! My fault!
move_uploaded_file($_FILES["newFile"]['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/img/uploads/' . $_FILES["newFile"]['name']);
Using $_SERVER['DOCUMENT_ROOT'] before the new file path worked perfectly.
Related
I am trying to use move_uploaded_file but I think everything is fine but my code is not working. Everything is working as expected even image name is being inserted into DB but it is not moving into uploads folder. Folder is there and apache2 does have access to write it.
I don't have enough reputation to post more then 8 lines so everything is on pastebin Here. The main aprt is...
$post_image_new_name = uniqid('', true). "."
.$post_image_actual_ext;
$post_image_destination = '../../uploads/';
if (is_dir($post_image_destination) && is_writable($post_image_destination)) {
$post_image_destination = '../../uploads/'.$post_image_new_name;
var_dump($post_image_destination);
move_uploaded_file($post_image_new_name, $post_image_destination);
echo "Inside move_uploaded_file section";
https://pastebin.com/Z321R76z
Try this code
line 53: move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);
The first param should be the file source name.
move_uploaded_file() expects the source filename of the uploaded file. So you must use something like this:
move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);
I'm trying to save a file inside php: // output to send it as an answer (it's an excel).
The problem is that php does not find the directory, according to the documentation should be able to access it.
i add this validation to my code:
$folderName = 'php://output';
if(!is_dir($folderName)){
throw new FileNotFoundException($folderName . " directory not found.");
}
$objWriter->save($filePath);
and the exception has been throwed and return me:
"php://output directory not found.",
php://output is not a directory; it's an output stream. You use php://output to write stuff to the output buffer the same way echo or print does. For example, if you wanted to force the browser to display a PDF or an image straight away without saving it first, you would use php://output.
If you wanted to physically save the file in your filesystem then a proper path must be used.
I am trying to move files from my FTP server to a local directory. First I need to find the correct file from the FTP server, as there can be a few hundreds:
//Search for the file.
$fileName= array_filter(Storage::disk('ftp')->files(), function ($file)
{
return preg_match('/('.date("Y-m-d" ,time()).').*.XLSX/', $file);
});
Above finds the correct file. If I dd($fileName), I get this:
My File Name.XLSX
I then try to move that file, to my public disk:
$ftp_file = Storage::disk('ftp')->get($fileName);
$local_file = Storage::disk('public')->move($ftp_file, "moved_file.xlsx");
However above code doesn't work. I get below error:
preg_match() expects parameter 2 to be string, array given
Which I have identified being on below function:
$ftp_file = Storage::disk('ftp')->get($fileName);
What am I doing wrong? How can I move the file, which I am able to find on my FTP server, to my local disk?
Thank you in advance.
As #Loek pointed out, $fileName was an array, and therefore to access it I needed to use:
$ftp_file = Storage::disk('ftp')->get($fileName[0]);
I am trying to move an image file previously uploaded into a tmp directory to a permament location. I have used this tutorial as a starting point:
maxoffsky.com/code-blog/uploading-files-in-laravel-4/
I have amended the code as I am uploading the files earlier and accessing them later through a url provided by the form. The code below shows what I am doing:
$form_element = "image_1"; // hidden form element containing the tmp location
$tmp_path = $form_data[$form_element]; // gets the tmp url from hidden element
$file = fopen($tmp_path, 'r'); // opens the file
$destinationPath = 'images/adverts/'.$advert->id; // specifies a new folder
$filename = $file->getClientOriginalName(); // retrieves the name of the file
$upload_success = $file->move($destinationPath, $filename); // moves the file to its new location
The problem I am having is that in the tutorial, they use the code:
$file = Input::file('file'); // Laravel syntax for $_POST['file']
This retrieves the file from the html form itself. From there the functions $file->getClientOriginalName() and $file->move() work correctly.
However, in mine, as my form doesnt provide an actual file, just a link to one, I am trying to access the file and perform the same operations, however I get this error:
Call to a member function getClientOriginalName() on a non-object
I dont think that fopen() is returning the same type as $_POST['file'] hence it isnt working.
How can I make my code work?
Many thanks
I had the same issue, just add in the Form::open in array the enctype:
enctype='multipart/form-data'
and your problems are solved! I have forgotten it and banging my head to the wall after that I started harder to bang it when I saw it.
PS. You have a little misconception. Input::file('file') it's not syntax for $_POST['file'], but for $_FILES['file']
I'm trying to upload some photos and handle this with the build in Laravel functions. But I can't for the life of me figure out how to do this properly. I have been able to actually upload something, but I've run into a few problems. This is the code I have right now:
If looked at the documentation, and found this function: $file = Input::file('photo'); I've used this function, and what the content of $file becomes is an instance of Symfony\Component\HttpFoundation\File\UploadedFile, which, as the documentation tells us, "extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file." http://laravel.com/docs/4.2/requests#files
Then I used this function Input::file('photo')->move($destinationPath); which should but the file in the desired folder on the server. And it did. But now comes the problem. Now all uploaded files have a filename like phpgoJnLc, and without an extension.
I've looked at the functions available from SplFileInfo and tried getExtension which give me an empty string and getFilename which also gives me something like phpgoJnLc.
Then I looked around on the internet and found a few part of code from Laravel 3, where they did something like this:
$filename = Str::random(20) .'.'. File::extension(Input::file('photo.name'));
But the result of this is give me only the result from Str::random(20) followed by a dot. Again, no file extension.
So, what am I doing wrong? How to upload a file with Laravel 4?
Looking in that same class file I see a getClientOriginalName() function...
$file = Input::file('photo');
$file->move($destinationPath,$file->getClientOriginalName());
... which ais ssuming you want to keep the original name your client sets... which could be hazardous, do some safety checks on it would be my advice. Getting the extensionname only is done with ->getClientOriginalExtension(), so you could also only save that part & add a random string before that in the second argument of the move() function.
This worked for me, especially when you want to change the name of the uploaded image:
$filename = 'New_Name'.'.'.Input::file('photo')->getClientOriginalExtension();
You can also generate a name for the file with the original file extension like so:
$filename = Str::random(20) . '.' . Input::file('image')->guessExtension();
if you try to use the following in Laravel 4:
$filename = Str::random(20) .'.'. File::extension(Input::file('photo.name'));
you will get this error:
'Call to undefined method Illuminate\Filesystem\Filesystem::guessExtension()'