I have an upload form that has two file input's.
I want to rename the files so that each have a unique name. Here is what I have in my Controller
public function mainAction()
{
$upload = new Zend_File_Transfer();
$files = $upload->getFileInfo();
foreach ($files as $file => $info) {
$upload->addFilter('Rename', uniqid($file.'_').'.csv', $file);
}
$upload->receive();
}
Even though I have specified the file as the last paramter in setFilter, it renames both files at the same time so that they end up with the same name.
I figured out how to do it.
This is the form:
<input type="file" name="one">
<input type="file" name="two">
this goes in the controller
$renamefile1 = new Zend_Filter_File_Rename(array(
'target' => $path.'/file1.csv', // path to file
'overwrite' => true
));
//rename file 2 to file2
$renamefile2 = new Zend_Filter_File_Rename(array(
'target' => $path.'/file2.csv', // path to file
'overwrite' => true
));
$names = $upload->getfileName();
$file1 = $renamefile1->filter($names["one"]);
$file2 = $renamefile2->filter($names["two"]);
Related
I'm trying to store multiple images to the database but it only stores one image. I don't see any errors. I have tried this but it stores images in public folder, I want the images to be stored in database. How can I fix this? any help would be appreciated.
Controller
if($request->hasFile('files')){
$files = $request->file('files');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$images = $file->store('public/photos');
}
}
ProductsPhoto::create([
'product_id' => $product->id,
'filename' => $images
]);
Blade
<input type="file" name="files[]">
I see one issue on your code, you are moving images correctly but storing it in DB outside of loop, which will only store last iteration of loop in DB, you can use this code to store multiple images in DB
if($request->hasFile('files')){
$store_file = [];
$files = $request->file('files');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$images = $file->store('public/photos');
$store_file[] = [
'product_id' => $product->id,
'filename' => $images
];
}
ProductsPhoto::insert($store_file);
}
In my laravel-application I have a <form> where it is possible to upload multiple files. When I submit the form the multiple files get stored into the database, but the table column attachment, which is supposed to store the path of the file, always displays 1 (true).
if (request()->has('attachment_files')) {
$files = request()->attachment_files;
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$filesize = $file->getClientSize();
$path = Storage::disk('local')->put('attachments' . $filename, $extension);
$data = SingleApplicationFile::create([
'files_id' => $application->id,
'single_application_id' => $application->id,
'attachment' => $path,
'attachment_name' => $filename,
'attachment_size' => $filesize,
]);
$attachment_file[] = $data;
new SingleApplicationFile($attachment_file);
}
}
As mentioned, the line $path = Storage::disk('local')->put('attachments' . $filename, $extension); always gives me true and in the database column a "1" is stored.
I used this method before for single file upload, and in that case, the mentioned line stores attachments/somefilename.pdf - so what is the issue here
put() returns a boolean value, which is why you are seeing a 1 in your database column and not the string path to your stored file. You might be thinking of putFile() instead.
Next you need to run
php artisan storage:link
and then you can retrieve the file as
{{ asset('storage/attachments' . $filename) }}
so you may like to save as
if ( Storage::disk('local')->put('attachments' . $filename, $extension) ) {
$path = 'storage/attachments' . $filename;
}
I'm trying to upload a file to Google Drive using API. Uploading process works fine but the problem is the uploaded file on the drive is empty(has nothing to show). Why?
Here is the uploading PHP code.
DEFINE("TESTFILE", $_FILES['myFile']);
DEFINE('TYPE', $_FILES["myFile"]["type"]);
$file = new Google_Service_Drive_DriveFile();
$file->setName('HelloWorld');
$file->setDescription('A test document');
$result = $service->files->create(
$file,
array(
'data' => TESTFILE,
'mimeType' => TYPE,
'uploadType' => 'media'
)
);
}
catch(Exception $e){
echo $e->getMessage();
}
And this is the HTML form:
<div>
<form enctype="multipart/form-data" method="POST">
<input type="file" name= "myFile">
<input type ="submit">
</form>
You are missing an intermediary step in your code.
You need to save the uploaded file to disk locally first ( as a temporary file ) and then read that file back into either a variable or file handle which you would then pass in place of TESTFILE in your $service->files->create() call.
Something like this ...
$file_tmp = $_FILES["myFile"]["tmp_name"];
$file_type = $_FILES["myFile"]["type"];
$file_name = basename($_FILES["myFile"]["name"];
move_uploaded_file($file_tmp, "path/to/upload/".$file_name);
$file_data = file_get_contents("path/to/upload/".$file_name);
$file = new Google_Service_Drive_DriveFile();
$file->setName('HelloWorld');
$file->setDescription('A test document');
$result = $service->files->create(
$file,
array(
'data' => $file_data,
'mimeType' => $file_type,
'uploadType' => 'media'
)
);
But please remember to validate your file uploads for proper type, size, etc. as best practice.
I am building a form that allows users upload more than one file provided they click on the 'Add more files' link. When I upload more than one file, for example, 2 files, I see the 2 files in the folder I store files but in database table, its only the first of the 2 files that is saved on the table. And it is saved twice. For instance, if I upload 3 files, the first of the 3 files is saved 3 times on the table but the 3 individual files I can see in the folder. Whats the problem here?
My view:
<label class="control-label mb-10" for="exampleInputuname_1">Uploads</label>
<input type="file" class="form-control" id="exampleInputuname_1"
name="file[]" required>
<a class="add_more" href="">Add More Files</a>
JQuery
$( function() {
$('.add_more').click(function(e){
e.preventDefault();
$(this).before("<input name='file[]' type='file' class='form-control'/>");
});
});
My controller
foreach ($files as $file) {
$file_extension = $file->getClientOriginalExtension();
$new_name = base64_encode(date('Y-m-d H:i:s') . $file->getClientOriginalName()) . '.' . $file_extension;
$destinationPath = 'uploads/files';
$file->move($destinationPath, $new_name);
$filename = $destinationPath . '/' . $new_name;
$i++;
$uri = Utilities::generateFileRef($i);
File::create([
'uri' => $uri,
'user_id' => $user_id,
'title' => $request->folder_name,
'document' => $filename,
'folder_id' => $folder_id
]);
}
I have a bit of the problem with file upload. I am trying to upload two files, but for some reason first file is being uploaded twice and there is no trace of the second file. What am I doing wrong?
Function for file upload (model):
public function file_upload($folder, $allowed_type, $max_size = 0, $max_width = 0, $max_height = 0)
{
$folder = $this->path . $folder;
$files = array();
$count = 0;
foreach ($_FILES as $key => $value) :
$file_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
$file_name = $this->global_functions->char_replace($file_name, '_');
$count++;
$config = array(
'allowed_types' => $allowed_type,
'upload_path' => $folder,
'file_name' => $file_name,
'max_size' => $max_size,
'max_width' => $max_width,
'max_height' => $max_height,
'remove_spaces' => TRUE
);
$this->load->library('image_lib');
$this->image_lib->clear();
$this->load->library('upload', $config);
if (!$this->upload->do_upload($key)) :
$error = array('error' => $this->upload->display_errors());
return FALSE;
else :
$file = $this->upload->data();
$files[] = $file['file_name'];
endif;
endforeach;
if(empty($files)):
return FALSE;
else:
return implode(',', $files);
endif;
}
Function form file upload (controller):
public function dokument_insert()
{
$this->admin_login_check();
$dokument =explode(',', $this->doc->file_upload('/doc', 'pdf|PDF|doc|docx') );
var_dump($dokument);
$data = array(
'naziv' => $this->input->post('naziv_srb'),
'opis' => $this->input->post('opis_srb'),
'path_pdf' => $dokument[0],
'path_doc' => $dokument[1],
'kategorija_id' => $this->input->post('kategorija'),
'jezik_id' => $this->input->post('jezik')
);
$this->doc->save($data);
}
Part of the form for files (view):
<label for="dokument_pdf">Dokument PDF</label>
<input type="file" name="pdf" id="dokument_pdf">
<label for="dokument_doc">Dokument DOC</label>
<input type="file" name="doc" id="dokument_doc">
You must to initialize the upload library with each upload.
$this->load->library('upload');
$this->upload->initialize($config);
User guide: https://www.codeigniter.com/user_guide/libraries/file_uploading.html
Looks like because you have 2 doc fields it will ANYWAY loop 2 times, whatever...
If you want only one you should send only one via the form OR don't use a ForEach and process each one manually each other with their fixed names.