I have .csv file which has two columns: name and http link:
name,link
IN0895,http://sample.com/images/example.jpg
IN0895,http://sample.com/images/example2.jpg
IN0872,http://sample.com/images/name.jpg
IN0872,http://sample.com/images/screen.jpg
I want create folder with name from first column and download file there (from second column). If folder already exists, just download file and put there.
How this can be done using bash, wget, curl or something else of your choice?
Fields in CSV file are comma separated in your example.
filename for CSV file is given at commandline when you call the script.
modify script permissions.
test it in a temp folder first so you don't clutter and have to clean up if it doesn't work.
not tested
#!/usr/bin/env bash
filename="$1"
while IFS="," read f1 f2
do
mkdir -p "$f1";
wget -P "$f1" "$f2"
done < "$filename"
mkdir -p checks if directory exists and makes directory if it doesn't
wget -P is Prefix (Parent folder) for folder to download to in case you are downloading more than one thing from URL.
f1 and f2 are the 2 fields in CSV file. f1 is the first field which will be the directory name and f2 is the URL.
I wrote something that does this in python. Keep in mind you have to install wget via pip install wget first.
import pandas as pd
import wget
#read in data
data = pd.read_csv("file.csv")
# assuming you have a column named Column1 which contains the link, iterate #through and download
for index, row in data.iterrows():
link = wget.download(row['Column1'])
<?php
// This is the class which is use for cURL operation..
class curl_image {
// Here two variable $name for Rename image, $img_url take image path
function image($name,$img_url)
{
// Here we define a file path where download image will save...
$path = "E:/xampp/htdocs/beauty_code_image/";
// Now initialize curl instance here with related method
$ch = curl_init($img_url);
$fp = fopen($path . $name, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
// cURL excute if above information is right otherwise show error msg
$result = curl_exec($ch);
// print_r($result); it just for display cURL is executed or not
curl_close($ch); // Close cURL here
fclose($fp);
}
}
// Initialize class object here
$obj = new curl_image();
// Here we check file is exist
if(isset($_FILES['file']['name']))
{
// We check here data is in valid mentioned format or not
$csvMimes = array('application/vnd.msexcel','text/plain','text/csv','text/tsv');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
//open uploaded csv file with read only mode
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
// fetch csv file here using Php inbuild function
fgetcsv($csvFile);
while(($line = fgetcsv($csvFile)) !== FALSE){
// Here object fetch the method which download image & rename it with SKU name
$obj->image($line[0].'.jpg',$line[1]);
}
}
// Close CSV file
fclose($csvFile);
}
}
?>
<html>
<head></head>
<body>
<div class="panel panel-default">
<div class="panel-body">
<form action="" method="post" enctype="multipart/form-data" id="importFrm">
<input type="file" name="file" />
<input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT">
</form>
</div>`enter code here`
</div>
</body>
</html>
Related
I want to create search file form, where user search files based on year.I already made folder file in a years (ex. folder 2019, folder 2018, etc), so when user input value the results will show based on user input. I get the result that i want, but i can't download file as zip because the value og path folder is null. I already tried use input-> get and session-> set_flashdata, but the result still null. My question is how do I get the year value, so can direct to the path folder?
Note : tahun is years in english
Controller
public function download_zip() {
// Read files from directory
$tahun = $this->input->get('tahun');
if($this->input-post('but_createzip2') != NULL){
// File name
$filename = $tahun . _laporan.zip;
// Directory path (uploads directory stored in project root)
$path = './uploaded/laporan/'.$tahun.'/';
// Add directory to zip
$this->zip->read_dir($path, FALSE);
// Save the zip file to archivefiles directory
$this->zip->archive('./uploaded/backup_laporan/'. $filename);
// Download
$this->zip->download($filename);
}
// Load view
$this->load->view('v_detail_laporan');
}}
View
<form role="form" action="<?php echo base_url().'laporan'?>">
<input type = "text" id="tahun" name="tahun" class="form-control" placeholder="Masukkan Tahun" required/>
</form>
// Download
<?php echo form_open('laporan/download_zip'); ?>
You are posting dat using GET method then why are you made condition in POST method
public function download_zip() {
// Read files from directory
$tahun = $this->input->get('tahun');
if($this->input->get('tahun') != NULL){ // MODIFIED HERE YOU ARE PASSING VALUES USING GET METHOD
// File name
$filename = $tahun ."_laporan.zip";
// Directory path (uploads directory stored in project root)
$path = './uploaded/laporan/'.$tahun.'/';
// Add directory to zip
$this->zip->read_dir($path, FALSE);
// Save the zip file to archivefiles directory
$this->zip->archive('./uploaded/backup_laporan/'. $filename);
// Download
$this->zip->download($filename);
}
// Load view
$this->load->view('v_detail_laporan');
}
Try this code
You have a wrong form target, try to modify the codes be like below.
View :
<?php echo form_open('laporan/download_zip', ['method' => 'post', 'role' => 'form']); ?>
<?php echo form_hidden('but_createzip2','1');?>
<input type = "text" id="tahun" name="tahun" class="form-control" placeholder="Masukkan Tahun" required/>
<?php echo form_close(); ?>
Controller :
public function download_zip() {
// Read files from directory
$tahun = $this->input->post('tahun');
if($this->input-post('but_createzip2') != NULL){
// File name
$filename = $tahun . _laporan.zip;
// Directory path (uploads directory stored in project root)
$path = './uploaded/laporan/'.$tahun.'/';
// Add directory to zip
$this->zip->read_dir($path, FALSE);
// Save the zip file to archivefiles directory
$this->zip->archive('./uploaded/backup_laporan/'. $filename);
// Download
$this->zip->download($filename);
}
// Load view
$this->load->view('v_detail_laporan');
}
I can't figure out how to get a database file attachment, to be downloaded with is real file name.
My model have many file attachements (many attachOne) and there is no problem to get link to them with
{{ model.myfile.filename }}
What I want to do is to get those files downloaded with their real file name.
I try to define an ajax event handler in my layout like so :
function onDonwload()
{
$path = post('path');
$name = post('name');
// Storage::exists('uploads/public/5ce/28c/3aa/5ce27c3aae590316657518.pdf'); => OK
// Storage::exists($path); =>OK
$path = storage_path().'/app/'. $path;
return Response::download( $path, $name);
}
and
<button data-request="onDonwload"
data-request-data="path: 'uploads/public/5ce/28c/3aa/5ce27c3aae590316657518.pdf', name: 'my real name">
Download
</button>
No missing file error, but get the browser to freeze with an alert that say "A webpage slow down your browser, what do you want to do?".
Did I miss an important point?
You should add separate page for downloading files, Ajax can not help you to download file ( may be it can but process is little complex and long)
create page with /file-download/:id here you can specify any url wit param :id and give it name file-download you can give any name you like for demo i used this name.
In that Page Html section will be Blank and in Page's code section add this code. here you can also check additional security check like user is logged in or not file is of related user or not. for now i am just checking file is related to particular Modal or not.
function onStart() {
$fileId = $this->param('id');
$file = \System\Models\File::find($fileId);
// for security please check attachment_type == your model with namespace
// only then lat use download file other wise user can download all the files by just passing id
if($file && $file->attachment_type == 'Backend\Models\User') { // here add your relation model name space for comparison
$file->output('attachment');
}
else {
echo "Unauthorised.";
}
exit();
}
Generating links, please replace page name with page you created.
<a href="{{ 'file-download'|page({'id': model.myfile.id}) }}" >{{ model.myfile.filename }}</a>
Now, when you click on link file should be downloaded with its original name, and for invalid file id it should show message Unauthorised..
if any doubts please comment.
i have upload section that stores files name in user's table, and files in Folder named as File.
i want to access that file from name stored in database.
i.e if it is a zip file it should get downloaded, if doc, pdf etc it should get open online.
my code in controller that fetches files:
$users = DB::table('users')->select('attachments')->where('id',$id)->first();
$attach = explode(",", $users->attachments);
return View::make('admin.RegisterStaff.show')
->with('attach', $attach);
my code in blade file to display files:
<div class="col-md-4">
#foreach($attach as $data)
{{ $data }}<br>
#endforeach
</div>
what is the best way to access files..??
You can try
#if($data->getClientOriginalExtension() == 'docx')
//do anything you want
#endif
i have some doubts in zipping the folder in codeigniter.Actually i have some image files in the folder and i was trying to zip this folder in codegiter. I have view file where i have written "Download all file" button. On clicking this button i have to take the folder and zip it.
My view file:
I have written the button "Download All Files" inside the anchor tag.
<tr>
<td align="right">
<?php if(isset($AppData) && !empty($AppData)) {
$applicantNo = $AppData[0]['appid']; ?>
<a href='<?php echo base_url("admin/downloadAllFileZip/$applicantNo");?>'><input type="submit" name="download" value="Download All Files" /></a>
<?php } ?>
</td>
</tr>
Controller code:
Inside admin.php controller file i have the below method
public function downloadAllFileZip($appId)
{
$path = file_get_contents(base_url().'files/'.$appId);
$applicant_id = $appId;
$this->zip->add_data($applicant_id,$path);
$this->zip->archive('/files/'.$applicant_id);
$this->zip->download($applicant_id);
}
But when I am clicking the " Download all file " button the popup window for zipping is comming and after downloading its showing 1kb file but there is no image on that file. I can able to zip single image file as a zip file but not able to zip the entire folder. Please help me how to do this.
add your all file like this. For Ex:
foreach ($query->result() as $row)
{
$this->zip->read_file($row->filename);
}
$this->zip->download('files_backup.zip');
To archive all files within a folder use $this->zip->read_dir(); method. Example,
$path = '/path/to/folder';
$this->zip->read_dir($path);
$this->zip->archive('/path/to/folder/foldername.zip');
For more info. please have a look: https://www.codeigniter.com/user_guide/libraries/zip.html
Zip library permits you to create Zip archives
$this->load->library('zip');
Give path of your folder that you want to add in archive
$file_name='backup['.date('d-m-Y-H:i:s').'].zip';
$this->zip->read_dir($_SERVER['DOCUMENT_ROOT']."/uploads/images",FALSE);
$this->zip->download($file_name);
I think give absolute path is better way for archived folder on specific folder
$this->load->library('zip');
$path = '/path/to/your_directory/';
$this->zip->read_dir($path);
$this->zip->download('my_backup.zip');
So use is uploading a logo and it's path is stored in a database like this:
C:\xampp\htdocs\laravel\public\logo\1496912432.jpg
I am displaying the image like this:
<img class="images" id="image" src="{{$business->image}}" />
However I get this error:
Not allowed to load local resource: file:///C:/xampp/htdocs/laravel/public/logo/1496912432.jpg
How can this problem be solved?
//edit
Controller:
public function image(Request $request) {
if($request->hasFile('img'))
{
$image = Input::file('img');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('logo/' . $filename);
Image::make($image->getRealPath())->fit(303, 200)->save($path);
$file = $request->file('img');
$session = session()->get('key');
$update_image = Business::find($session);
$update_image->image = $path;
$update_image->save();
return ['url' => url('logo/' . $filename)];
}
Use Laravel file() to store files https://laravel.com/docs/5.4/requests#files
Store the $path to your db
$path = $request->photo->store('logo');
the $request->photo is depending on your input file attribute name. In your case, it should be $request->img.
the above code will create a folder (if not exist), namely "logo" and store to that folder with random string file name.
Also check your configuration for file, located at /config/filesystem.php. Default is set to public
Use asset function to get the full path from public folder
<img class="images" id="image" src="{{ asset($business->image }}" />
You can do in two ways
Best way is update url path when image saving save url path to db
$path = $request->photo->store('logo'); // in 5.4
The other way if you can't changes db url you can do some hack like this
$file = explode('/public/', $business->image);
echo asset($file[1]);
You want to store all files inside the web root. Because of cross-domain security, you cannot access the file:// domain/protocol from a http protcol. By using Laravel to store and retrieve, it will come from the same host.