I'm working on HRMS project, In that I'm saving employees data, Storing like ID Proof, Resume etc. But while uploading it gives me error as in screenshot.
Code:
$files= [];
if($request->hasfile('resume'))
{
$files[] = $resumeFilename;
}
if($request->hasfile('profile_photo'))
{
$files[] = $imagefilename;
}
if($request->hasfile('id_proof'))
{
$files[] = $id_proof;
}
if($request->hasfile('prevEmpType'))
{
$files[] = $prevEmpType;
}
if($request->hasfile('offer_letter'))
{
$files[] = $offer_letter;
}
if($request->hasfile('con_a_agree'))
{
$files[] = $con_a_agree;
}
foreach ($files as $file)
{
$file->move('uploads/' , $file); //Error is here i.e "Call to a member function move() on string"
}
Uploading single file from each input type file field as in below screenshot.
Error:
How to upload file in folder at once from multiple input filed?
You can use Storage facade to save a file data
$files= [];
if($request->hasfile('resume')) $files[] = $resumeFilename;
if($request->hasfile('profile_photo')) $files[] = $imagefilename;
if($request->hasfile('id_proof')) $files[] = $id_proof;
if($request->hasfile('prevEmpType')) $files[] = $prevEmpType;
if($request->hasfile('offer_letter')) $files[] = $offer_letter;
if($request->hasfile('con_a_agree')) $files[] = $con_a_agree;
// save the files
foreach ($files as $file) {
$filename = sprintf('%s.%s', md5(\Str::random(5)), $file->getClientOriginalExtension());
Storage::disk('uploads')->putFile($file, $filename);
}
You can get the file response with
public function foo(string $filename)
{
return response()->file(storage_path("app/uploads/$filename"));
}
Please read this reference about using File Storage in Laravel
Related
I have two laravel project i use code in my Myfirstproject to upload to Mysecondproject public\src\img\upload directory.
I try these code in Myfirstproject:
if ($request->hasFile('images')) {
$destinationPath='Mysecondproject\public\src\img\upload';
if ($files = $request->file('images')) {
foreach ($files as $file) {
$name = $file->getClientOriginalName();
$file->move($destinationPath, $name);
$images[] = $name;
}
}
}
But it's not working, any solution for these?
Please try absolute path in $destinationPath as C:\Mysecondproject\public\src\img\upload or D:\Mysecondproject\public\src\img\upload.
How to create Zip archive for file only particular extension in my case .TIF .JPG .TXT
currently i have added single extensions in code you can modify it later on.
<?php
/* creates a compressed zip file */
function create_zip($files = array(), $destination = '', $overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if (file_exists($destination) && !$overwrite) {
return false;
}
//vars
$valid_files = array();
//if files were passed in...
if (is_array($files)) {
//cycle through each file
foreach ($files as $file) {
//make sure the file exists
if (file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if (count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach ($valid_files as $file) {
$zip->addFile($file, $file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
} else {
return false;
}
}
//your directory path where all files are stored
$dir = 'D:\xampp\htdocs\samples\zip';
$files1 = scandir($dir, 1);
// $ext = "png"; //whatever extensions which you want to be in zip.
$ext = ['jpg','tif','TXT']; //whatever extensions which you want to be in zip.
$finalArray = array();
foreach ($files1 as $key => $value) {
$getExt = explode(".", $value);
if ( in_array($getExt[1] , $ext) ) {
$finalArray[$key] = $value;
}
}
$result = create_zip($finalArray, 'my-archive' . time() . '.zip');
if ($result) {
echo "Operation done.";
}
?>
i think this is what you want.
let me know if you have any issue.
I'm trying to get all of the text from diffrent text files in a certain folder using PHP. I've already figured out how to do it for all the images in a folder but now i need a way to do the same for text files. This is the code i have so far.
<?php
$dir = 'Nieuws';
$file_display = ['txt'];
if (file_exists($dir) == false) {
return ["Directory \'', $dir, '\' not found!"];
} else {
$dir_contents = scandir($dir);
foreach ($dir_contents as $file) {
echo $file;
echo file_get_contents($file);
}
}
?>
How can I get all the files in my directory on php and put them in one document called "combined.txt"
I had this code before:
file_put_contents("combined.txt", ""); // Empty the file first
foreach ($files as $my_file) {
file_put_contents("combined.txt", $my_file, FILE_APPEND);
}
But I get this error:
Warning: Invalid argument supplied for foreach() in /script2.php on
line 177
I think its because I didn't delceare which files, I just have this code before it:
$directory_with_files = './'.date('m-d-Y');
$dh = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
if(in_array($filename, array('.', '..')) || is_dir($filename))
continue;
$files[] = $filename;
}
Any ideas?
You can achive this by using scandir() function
$directory_with_files = './'.date('m-d-Y');
$files = scandir($directory_with_files);
$valid_extension=array('txt','php','inc')// make a list of valid extention
foreach($files as $file)
{
$ext=explode('.',$file);
$ext=strtolower(array_pop($ext))
if(in_array($ext,$valid_extension))
{
include_once($directory_with_files."/".$file);
}
}
i am trying to create a zip file(using php) for this i have written the following code:
$fileName = "1.docx,2.docx";
$fileNames = explode(',', $fileName);
$zipName = 'download_resume.zip';
$resumePath = asset_url() . "uploads/resume/";
//http://localhost/mywebsite/public/uploads/resume/
$zip = new ZipArchive();
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
echo json_encode("Cannot Open");
}
foreach ($fileNames as $files) {
$zip->addFile($resumePath . $files, $files);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zipName."");
header("Content-length: " . filesize($zipName));
header("Pragma: no-cache");
header("Expires: 0");
readfile($zipName);
exit;
however on a button click i am not getting anything..not even any error or message..
any help or suggestion would be a great help for me.. thanks in advance
Why not use the Zip Encoding Class in Codeigniter - it will do this for you
$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);
// Write the zip file to a folder on your server. Name it "my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip');
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
https://www.codeigniter.com/user_guide/libraries/zip.html
... it work for me
public function downloadall(){
$createdzipname = 'myzipfilename';
$this->load->library('zip');
$this->load->helper('download');
$cours_id = $this->input->post('todownloadall');
$files = $this->model_travaux->getByID($cours_id);
// create new folder
$this->zip->add_dir('zipfolder');
foreach ($files as $file) {
$paths = 'http://localhost/uploads/'.$file->file_name.'.docx';
// add data own data into the folder created
$this->zip->add_data('zipfolder/'.$paths,file_get_contents($paths));
}
$this->zip->download($createdzipname.'.zip');
}
What is asset_url() function? Try to use APPPATH constant istead this function:
$resumePath = APPPATH."../uploads/resume/";
Add "exists" validation for file names:
foreach ($fileNames as $files) {
if (is_file($resumePath . $files)) {
$zip->addFile($resumePath . $files, $files);
}
}
Add exit() after:
echo json_encode("Cannot Open");
Also I think it's the better desision to use CI zip library User Guide. Simple example:
public function generate_zip($files = array(), $path)
{
if (empty($files)) {
throw new Exception('Archive should\'t be empty');
}
$this->load->library('zip');
foreach ($files as $file) {
$this->zip->read_file($file);
}
$this->zip->archive($path);
}
public function download_zip($path)
{
if (!file_exists($path)) {
throw new Exception('Archive doesn\'t exists');
}
$this->load->library('zip');
$this->zip->download($path);
}
Below scripting working ok in my local system. 1st remove asset_url() from $resumePath and set zip file store location relative path.
- Pass zip file name with its location path to $zip->open()
$fileName = "1.docx,2.docx";
$fileNames = explode(',', $fileName);
$zipName = 'download_resume.zip';
$resumePath = "resume/";
$zip = new ZipArchive();
if ($zip->open($resumePath.$zipName, ZIPARCHIVE::CREATE) !== TRUE) {
echo json_encode("Cannot Open");
}
foreach ($fileNames as $files) {
$zip->addFile($files, $files);
}
$zip->close();
/* create zip folder */
public function zip(){
$getImage = $this->cart_model->getImage();
$zip = new ZipArchive;
$auto = rand();
$file = date("dmYhis",strtotime("Y:m:d H:i:s")).$auto.'.zip';
if ($zip->open('./download/'.$file, ZipArchive::CREATE)) {
foreach($getImage as $getImages){
$zip->addFile('./assets/upload/photos/'.$getImages->image, $getImages->image);
}
$zip->close();
$downloadFile = $file;
$download = Header("Location:http://localhost/projectname/download/".$downloadFile);
}
}
model------
/* get add to cart image */
public function getImage(){
$user_id = $this->session->userdata('user_id');
$this->db->select('tbl_cart.photo_id, tbl_album_image.image as image');
$this->db->from('tbl_cart');
$this->db->join('tbl_album_image', 'tbl_album_image.id = tbl_cart.photo_id', 'LEFT');
$this->db->where('user_id', $user_id);
return $this->db->get()->result();
}