Renaming input file before uploading with jquery and laravel - php

I want to rename input file to be uploaded before sending to laravel.
Basically, i found an another way to rename the file in laravel but in this question I want to rename the file before sending to laravel.
In my case, I'm using jquery upload file, and now I want the input file to be renamed before uploading it. I want to get the new file name that I used to insert in the hidden input text.
Is there any solution to solve this kind of matter?
By the way, thank you in advance! ^_^

Did you check this: File Docs
as per this doc you can do this as follow:
$request->file('photo')->move($destinationPath, $fileName);
where $fileName is an optional parameter that renames the file.
so you can use this like:
$fileName = str_random(30); // any random string
then pass this as above.

Related

How to upload,get and update an image or file in laravel ? What is the correct way?

What is the correct way to upload,update and get an image or file in laravel ? I had gone through laravel docs and other references but its confusing.Can anyone show a complete simple example for
any disk configuration in filesystems.php
a simple method to upload and update image in controller
and what will be the image full url?
input type file name is image you can store directly into images folder
return $path = $request->image->store('images');

Is it possible to upload file without input type="file"

I want to use a text-box, insert my file address is in the Desktop, and then upload it in the server, what should I do?
Is it because you dont like file uploader object how it looks?
If yes I suggest you to make it hidden but in background use its fuctionality.
Show file selected information in a textbox but keep the file into hidden file uploder object.
Another method to post files like audio and jpeg to server is to use Base64 format . Its consider as string but not recommanded for large files.
Then you need a backend function to save data into file object or simply insert data into database.
Do it this way:
$file_url= $_POST['file']; //Path of file to be uploaded
if file_exists($file_url):
$fh= fopen("NewfileName", a+);
fwrite($fh, file_get_contents($file_url));
fclose($fh);
endif;

Manipulating file in PHP without $_FILES

I'm trying to programmatically add a tint to an image. I've done this very easily with a form and the $_FILES array but I need to get the image from a URL, and the images I'm trying to use are on my own server if that is important. Here's a sample code of the working form function
$img = new Upload($_FILES['imgfile']);
Now all I need is something like $img = new Upload(fopen($image_path); But this isn't working... Please help me!!
In the following code, you are passing a $_FILES array structure to your Upload class:
$img = new Upload($_FILES['imgfile']);
The variable $_FILES['imgfile'] itself is an array, consisting of named members such as 'name', 'tmp_name' and others - information about the file that has been passed to PHP by the server receiving the uploaded file.
Your Upload class appears to be designed to handle this sort of structure. It will probably have been designed specifically for handling file uploads, so you may need to modify it a bit to allow it to be passed any file path as a parameter instead of this array structure.
Well, you can fake $_FILES array all right.
the only function you cannot fool with this array is move_uploaded_file() but it seems you don't need it anyway.
but, you know, nobody have an idea what your "new Upload" is.

I want to create a file that been given name by the user and save some image in it ? in php

i want to create a file.The name of the file is given by the user.So i take the textfield value to $name and how can i save a file with that name? i know how to save with a static name like
$myfile="ads.txt"
so my question is.is there a way that i can create a file with the name $name and save some stuff in it like image.
$file = fopen($name, 'wb'); // for the love of God and all that is holy, please validate $name
fwrite($file, $data);
fclose($file);
You can't save images in a txt file.
What you must do is save the actual image to your server (uploaded via a file field) and save to a database the location and name of the image, along with the user id, so you know who uploaded it.
There is quite a large amount of code involved, so i would suggest googling for "php file upload tutorial", plenty of examples out there.

is it possible to change the name of a file after it's been uploaded

Is there any way for a php script to choose the name for a file after it's been uploaded by the user using an HTML form? I am wanting to allow users to upload an avatar for their account and would like it named with their userid instead of whatever the name of it is on their computer. I'm using a basic HTML upload form which only allows jpegs and png files with a 10MB file limit, similar to the file upload code give on http://www.w3schools.com/php/php_file_upload.asp Any help you can give will be greatly appreciated.
Put the desired filename in the second argument of move_uploaded_file().
You can specify the filename when using move_uploaded_file(), otherwise you can rename() the file.
$userid = 5; // say you fetch it from database
$ext = explode("\/",$_FILES["file"]["type"]); //extract the file extension
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid.$ext[1]);
UPDATE:
I think you don't need to extract file extension.
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid);

Categories