Today i was found strange thing in file upload via symfony 3. I was trying upload file this way:
// get file
$uploadedFile = $request->files->get('uploadedFile', null);
// generate file name
$fileName = $this->getNewFileName();
// move file
$uploadedFile->move($path, $fileName.".".$uploadedFile->guessExtension());
But i get FileNotFoundException...
So i must do this: (Move method guessExtension out, before method move)
// get file
$uploadedFile = $request->files->get('uploadedFile', null);
// generate file name
$fileName = $this->getNewFileName().".".$uploadedFile->guessExtension();
// move file
$uploadedFile->move($path, $fileName);
Can anybody explain why?
EDIT:
File i has from classic <form> with enctype="multipart/form-data" with <input type="file">
Related
I'm new to Laravel 5.1
Can you guys help me on how to upload files like docx, PDF or image to store it in the database using Laravel 5.1 ?
I browsed a lot of tutorials but not in Laravel 5.1 I'm trying it myself but it didn't work.
NotFoundHttpException in
C:\Loucelle\ _\
_\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php line 161:
Can you help me by giving any sample codes?
Your Route:
Route::post('uploadFile', 'YourController#uploadFile');
Your HTML blade:
<form action="{{ url('uploadFile') }}" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
Your Controller:
public function uploadFile()
{
//get the file
$file = Input::file('file');
//create a file path
$path = 'uploads/';
//get the file name
$file_name = $file->getClientOriginalName();
//save the file to your path
$file->move($path , $file_name); //( the file path , Name of the file)
//save that to your database
$new_file = new Uploads(); //your database model
$new_file->file_path = $path . $file_name;
$new_file->save();
//return something (sorry, this is a habbit of mine)
return 'something';
}
Useful resources (these links may expire, so they are only here for reference):
Laravel Requests (like Inputs and the such)
File Upload Tutorial that helped me when I started
I'm uploading a file that is a zip in a web app and passing it as type "Part" and I need to grab the name of the file that I originally uploaded. I can't seem to figure out for the life of me how to grab the actual name of the uploaded file. I've tried the following assuming my Part is uploaded with the original file name as "ABCD". My Part object will be named "file":
file.getHeaderNames() yields "content-type" and "content-disposition"
file.getName() yields "BPzip8237267963573706108tmp" which is the temp file's name
Any ideas on how I would go about doing this?
// define variable for file name
String filename = "";
// get part
Part file = request.getPart("file");
// get filename from part header
for (String s: file.getHeader("content-disposition").split(";")) {
if (s.trim().startsWith("filename")) {
filename = s.split("=")[1].replace("\"", "");
break;
}
}
i have blueprint like below, and using flask-upload for uploading file
#blueprint.route('/', methods=['GET', 'POST'])
def upload_file1():
# user = User.query.filter_by(id=current_user.id).first_or_404()
form = PhotoFormUpload()
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
foto = form.photo_upload.data.lower()
filename = user_photos.save(foto)
update_avatar = User.query.filter_by(id=current_user.id).update(dict(avatar=filename))
db.session.commit()
flash('Upload Success', category='success')
return render_template('upload/display_photo.html', filename=filename)
else:
return render_template('upload/upload.html', form=form)
i change
foto = form.photo_upload.data
to
foto = form.photo_upload.data.lower()
but it doesnt works
how do i rename uploaded file name?
Answer on your question exist in http://pythonhosted.org/Flask-Uploads/
save(storage, folder=None, name=None)
Parameters:
storage – The uploaded file to save.
folder – The subfolder within the upload set to save to.
name – The name to save the file as. If it ends with a dot, the file’s extension will be appended to the end.
Example: user_photos.save(pathToDirectory, name=NewName)
I used the following method to rename the uploaded file on the fly
file = request.files['file']
file.filename = "abc.txt" #some custom file name that you want
file.save("Uploads/"+file.filename)
I am using WebTechNick's file upload plugin to save images on my CakePHP site. That part is working perfectly. I am working in my projects_controller in the add action I loop through the uploaded files and attempt to create and save thumbnails. The thumbnail creation goes well, but I get this error when trying to save the images to the thumbnail directory.
Unable to open 'files/project_images/thumbs' for writing: Is a directory
my files, project_images, and thumbs are all chmoded to 777 so I dont see why I am getting an "unable to open" error. My full code is below.
for($i=0; $i<=2; $i++){
$path = 'files/';
$imageName = $this->Project->Image->read('name', $imageStartingId);
$fullPath = $path . $imageName['Image']['name'];
list($width, $height) = getImageSize($fullPath);
$ratio = $width/$height;
$thumbnailHeight = $thumbnailWidth/$ratio;
//resample
$imageThumb = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
$image = imagecreatefromjpeg($fullPath);
imagecopyresampled($imageThumb, $image, 0,0,0,0, $thumbnailWidth, $thumbnailHeight, $width, $height);
imagejpeg($imageThumb, 'files/project_images/thumbs', 100);
$imageStartingId--;
}
any help is much appreciated. Thanks!
You need to give imagejpeg() a filename, and you've given it only the path, as the error message says. Change it to
imagejpeg($imageThumb, 'files/project_images/thumbs/'.$imageName['Image']['name'], 100);
or whatever you want the filename to be.
I have sucessfully managed to make a file upload system which basically is copying files to a specific folder and save in the database its location. Now i need help with the download part. Imagine my file location is: Files/1306242602661_file1.exe, and in my view i have this:
<g:link controller="fileManager" action="downloadFile">
Download</g:link><br>
I need help with the downloadFile controller. Could you please give me a hint about how to do this, considering my filename is a string:
String fileName = "Files/1306242602661_file1.exe"
Within your controller create an download action with following content:
def file = new File("path/to/file")
if (file.exists()) {
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition", "filename=${file.name}")
response.outputStream << file.bytes
return
}
// else for err message
You can render a file. see http://grails.org/doc/2.4.x/ref/Controllers/render.html
render file: new File ("path/to/file.pdf"), fileName: 'myPdfFile.pdf'