How can i access a file in local? - php

I am trying to get an image that is uploaded from the user.
Upload function:
public function updatedUpload($upload){
$object = $this->currentTeam->objects()->make(['parent_id' => $this->object->id]);
$object->objectable()->associate(
$this->currentTeam->files()->create([
'name' => $upload->getClientOriginalName(),
'size' => $upload->getSize(),
'path' => $upload->storePublicly('files', ['disk' => 'local'])
])
);
$object->save();
$this->object = $this->object->fresh();
}
This gets me this link in the database:
files/9yzPCLZzlT2aiogc8A8DQIdJTNrkiZ0eu0QTESFF.jpg
How can i access this through an image so i can see the picture?

While saving the file, you used the steatement:
$upload->storePublicly('files', ['disk' => 'local'])
This storePublicly() method return the path instead of the file itself. As a result the path where the file is stored is being shown. Which is not incorrect. Just check the returned file path and check if that file exists or not.
Now, what exactly you expect from the database? The file content? Or you want to show the file in html?
If you want to see the file content then you can use:
get_file_contents($this->object->path);
Or if you want to see the file in web page using blade, then:
<img src="{{$this->object->path}}">

The image will save on your disk in this address
http://your-site.com/storage/files/9yzPCLZzlT2aiogc8A8DQIdJTNrkiZ0eu0QTESFF.jpg
And you can access it in your blade file via this
asset_url("files/9yzPCLZzlT2aiogc8A8DQIdJTNrkiZ0eu0QTESFF.jpg")

Related

How do I call extract_to_pathname function on a user uploaded zip file?

I have an instance of an activity and when I want to view it I want to extract the associated zip file to be extracted and set the index.html file inside to be the starting page inside view.php.
I found out that the filepath I passed in the extract function doesn't exist, but I can access its fields in the database.
How do I properly pass in the filepath parameter?
Edit:
$fp = get_file_packer('application/zip');
$fileinfo = array(
'component' => 'mod_game',
'filearea' => 'content',
'itemid' => 0,
'contextid' => 472,
'filepath' => '/',
'filename' => 'game.7z');
$myfile = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'],
$fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']);
$filepath = '/'.$context->id.'/mod_game/content/'.$game->revision.$myfile->get_filepath().$myfile->get_filename();
$files = $fp->extract_to_pathname($filepath, $CFG->dirroot.'/mod/game/games'.$filepath.'_extracted');
I can see a number of issues here.
Firstly, the file ending (.7z) suggests that this is not a Zip file, but 7Zip file, which Moodle does not (as far as I am aware) support the unzipping of.
The next problem is that you don't appear to have created the folder into which you want to extract the files - you need to create the destination folder, before you try to extract files into it.
Finally, you appear to be trying to pass the (partial?) URL of the file to unzip, rather than either the path to it on the server (NOT recommended in this case) or the stored_file instance itself (strongly recommended).
Unless you need these long term, I would suggest a better solution would look like this:
// Make a temporary folder that will be automatically deleted at the end of the request.
$dest = make_request_directory();
// Extract the stored_file instance into this destination.
$files = $fp->extract_to_pathname($myfile, $dest);

Why is Laravel renaming the file extension of the image that I upload? [duplicate]

I am allowing users to upload any kind of file on my page, but there might be a clash in names of files. So, I want to rename the file automatically, so that anytime any file gets uploaded, in the database and in the folder after upload, the name of the file gets changed also when other user downloads the same file, renamed file will get downloaded.
I tried:
if (Input::hasFile('file')){
echo "Uploaded</br>";
$file = Input::file('file');
$file ->move('uploads');
$fileName = Input::get('rename_to');
}
But, the name gets changed to something like:
php5DEB.php
phpCFEC.php
What can I do to maintain the file in the same type and format and just change its name?
I also want to know how can I show the recently uploaded file on the page and make other users download it??
For unique file Name saving
In 5.3 (best for me because use md5_file hashname in Illuminate\Http\UploadedFile):
public function saveFile(Request $request) {
$file = $request->file('your_input_name')->store('your_path','your_disk');
}
In 5.4 (use not unique Str::random(40) hashname in Illuminate\Http\UploadedFile). I Use this code to ensure unique name:
public function saveFile(Request $request) {
$md5Name = md5_file($request->file('your_input_name')->getRealPath());
$guessExtension = $request->file('your_input_name')->guessExtension();
$file = $request->file('your_input_name')->storeAs('your_path', $md5Name.'.'.$guessExtension ,'your_disk');
}
Use this one
$file->move($destinationPath, $fileName);
You can use php core function rename(oldname,newName) http://php.net/manual/en/function.rename.php
Find this tutorial helpful.
file uploads 101
Everything you need to know about file upload is there.
-- Edit --
I modified my answer as below after valuable input from #cpburnz and #Moinuddin Quadri. Thanks guys.
First your storage driver should look like this in /your-app/config/filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'), // hence /your-app/storage/app/public
'visibility' => 'public',
],
You can use other file drivers like s3 but for my example I'm working on local driver.
In your Controller you do the following.
$file = request()->file('file'); // Get the file from request
$yourModel->create([
'file' => $file->store('my_files', 'public'),
]);
Your file get uploaded to /your-app/storage/app/public/my_files/ and you can access the uploaded file like
asset('storage/'.$yourModel->image)
Make sure you do
php artisan storage:link
to generate a simlink in your /your-app/public/ that points to /your-app/storage/app/public so you could access your files publicly. More info on filesystem - the public disk.
By this approach you could persists the same file name as that is uploaded. And the great thing is Laravel generates an unique name for the file so there could be no duplicates.
To answer the second part of your question that is to show recently uploaded files, as you persist a reference for the file in the database, you could access them by your database record and make it ->orderBy('id', 'DESC');. You could use whatever your logic is and order by descending order.
You can rename your uploaded file as you want . you can use either move or storeAs method with appropiate param.
$destinationPath = 'uploads';
$file = $request->file('product_image');
foreach($file as $singleFile){
$original_name = strtolower(trim($singleFile->getClientOriginalName()));
$file_name = time().rand(100,999).$original_name;
// use one of following
// $singleFile->move($destinationPath,$file_name); public folder
// $singleFile->storeAs('product',$file_name); storage folder
$fileArray[] = $file_name;
}
print_r($fileArray);
correct usage.
$fileName = Input::get('rename_to');
Input::file('photo')->move($destinationPath, $fileName);
at the top after namespace
use Storage;
Just do something like this ....
// read files
$excel = $request->file('file');
// rename file
$excelName = time().$excel->getClientOriginalName();
// rename to anything
$excelName = substr($excelName, strpos($excelName, '.c'));
$excelName = 'Catss_NSE_'.date("M_D_Y_h:i_a_").$excelName;
$excel->move(public_path('equities'),$excelName);
This guy collect the extension only:
$excelName = substr($excelName, strpos($excelName, '.c'));
This guy rename its:
$excelName = 'Catss_NSE_'.date("M_D_Y_h:i_a_").$excelName;

Laravel - Display preview of file stored in Storage directory

I have to upload some files for each users, and the files should not be accessible publicly.
When a user a created, I'm creating a folder in storage directory using-
Storage::makeDirectory($user->ref_id);
Now I've a files table, which stores the file details. Here is the code for uploading file and saving the path to the database.
$this->validate($request, [
'file' => 'required|mimetypes:image/png,image/jpeg,application/pdf',
]);
$user = Auth::user();
$filename = time() . '.' . $request->file->getClientOriginalExtension();
$path = $request->file('file')->storeAs($user->ref_id, $filename);
$user->files()->create(['file_name' => $path]);
The file is being stored successfully. Now when a user logs in, I want to display a preview of that file in the view.
Any help, how can I do that??
This is how you serve a file from a controller. Instead of returning a view you return the file like this. In case of an image you can't echo it directly to the view unless you base64 encode it (which increases the filesize).
return response()
->download($file_path, "file_name",
[
'Content-Type' => 'application/octet-stream'
]);
Make a function in your controller containing the code above and some logic to retrieve the right file path and make a route for it.
After you've done that you can (for example) add in your view
<img src="{{route('ROUTE_NAME')}}">
You'll have to fill in all the variables yourself ofcourse.
Using this method the files will always stay private and will only be echoed once you allowed the user access. Note that this WILL use more recourses as you let PHP handle serving the file instead of apache. Hope this helps!
This solved my problem...
https://stackoverflow.com/a/41322072/6792282
I used this in my code and preview is working fine for pdf files..
<embed name="plugin" src="{{url('/')}}/{{ Storage::disk('local')->url($file->file_name)}}" type="application/pdf">

Not allowed to load local resource (windows)

I am trying to upload some files in local disc C:\ and then read them and display. I have done the following:
at config.php
'route' => 'C:/',
the function that gets the path
function object_get_upload_path($type,$id){
$config = app_db_get_config();
return sprintf($config['route']."uploads/%s/%s/%d/", $_SESSION['active_db'],$type, $id);
}
The problem is that when click the link that gets the path of the file and displays it says:
Not allowed to load local resource: file:///C:/uploads/MARO_KONDI_AL/objekte/48167/laura1e11c10053fd1c6e5dca911103e5c3ae90072.jpg
Basically, if you want to load a file, you have to use the file_get_contents() function. Then you should have a look on the server side for the correct synthax.
I suppose the following one would be correct:
$config['route'] = 'C:/'; // or $_SERVER['DOCUMENT_ROOT']
$display = file_get_contents($config['route'].'uploads/'.$_SESSION['active_db'].'/'.$type.'/'.$id);

How to store a file in Moodle so that it is accessible for an external application?

I need to store a file in Moodle. This is not really a problem, it is explained here. The problem is that this file has to be accessible for everyone. Hence, there has to be a URL, e.g. www.mymoodlesite.com/temp/myfile.txt or the like, which one can enter in ones browser and access the file. I thought of copying the file into the moodledata/temp folder, but then I do not have a URL in order to access the file..
Thanks for your help in advance!
Finally I could solve my problem :-)
I used a filemanager like this:
$mform->addElement('filemanager', 'my_filemanager', 'Upload a file', null, array('maxbytes' => $CFG->maxbytes, 'maxfiles' => 1, 'accepted_types' => array('*.zip')));
Then saved the uploaded file like this:
if ($draftitemid = file_get_submitted_draft_itemid('my_filemanager')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_assignment', 'my_filemanager', 0, array('subdirs' => false, 'maxfiles' => 1));
}
The URL in order to access the uploaded file can then be created like this:
file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/mod_assignment/my_filemanager');
Assuming that you have added the element like this :
$mform->addElement('filepicker', 'file', "Upload a Document", null, array('maxbytes' => 1024*1024, 'accepted_types' =>array('*.png', '*.jpg', '*.gif','*.jpeg', '*.doc', '*.rtf','*.pdf','*.txt')));
Now assuming that You get the data as the following
$data = $lesson_form->get_data()
See the code below to upload the file to a specified folder in your server. This is compatible with moodle 2.2+
$realfilename = $lesson_form->get_new_filename('file'); // this gets the name of the file
$random =rand(); // generate some random number
$new_file = $random.'_'.$realfilename; //add some random string to the file
$dst = "uploads/$new_file"; // directory name+ new filename
if($realfilename !=''){ // checking this to see if any file has been uploaded
save_files($dst); // moodle function to save a file in given folder
}
I faced the same problem that you're facing and it solved my problem.
N.B. -> Remember to chmod your upload folder to 0777.
You can access files uploaded through moodle's file browser without being authenticated if the following is true
- Your moodle site has forcelogin set to no
- Your file is uploaded the the files in frontpage sitefiles.
Uploaded files are saved (assuming Moodle1.9) in moodledata/1/{filepath}. Since you have to do it programatically you can store them there and reference them using the url /file.php/1/{filepath}. To say it another way. Files saved to $CFG->datadir.'/1/'.filepath are accessible with $CFG->wwwroot.'/file.php/1/'.filepath;
Alternatively if you don't want the files to show up in your front page site files through the moodle file browser you could edit file.php to forget checking permissions for files located in your special directory and instead just serve them up.
Hope this is more helpful with this edit.

Categories