Cannot download File in Laravel 8 - php

I'm trying to download a file stored in my Larval 8 Storage folder. The path is correct I have checked it multiple times. And If I open the path in the search bar. It takes me to the needed image. What am I doing wrong here?
Controller
public function show($_id)
{
$currentUrl = URL::current();
$qrCode = QrCode::format('svg')->size(100)->generate($currentUrl."/".$_id);
$path = storage_path('app/public/'.$_id.'.png');
$asset = Asset::findorFail($_id);
return view('assets::asset',compact('asset', 'qrCode', 'path'));
}
asset.blade.php
<a href="{{storage_path('app\public/'.$asset->_id.'.png')}}" download>{{$qrCode}}</a>
The path of the image is: file:///D:/Facility_Management_System/storage/app/public/6155a180001300004e005e22.png
Which is same path as href="{{storage_path('app\public/'.$asset->_id.'.png')}}"
But, When I click to download the file to download it does nothing.

The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.
So in this case, if the downloaded file extension is .htm, it means that the response from specified link {{storage_path('app\public/'.$asset->_id.'.png')}} is html, instead of image file(maybe 404).
You need to fix the public url of the image file.

Related

hello I wanna use php_move_upload_file in order to move image from my temp folder to a permanent folder

hi this is the function that upload image inside temp location and save location to session for forther use
function uploadPhoto()
{
$rawImage = $_FILES['advPhoto'];
$uploader = new ImageUploader($rawImage);
$uploader->moveToProjectTempFolder();
//1 save the current image in sassion (save the attachment class inside seesion)
$uploader->saveInSession();
// $temperrary = $uploader->CurrentImgTemperraryLocation();
//2 send reponse the current image location
AjaxHelper::sendAjaxResponse("images/temp/" . $uploader->CurrentImgTemperraryLocation());
//create image tag and set the image source the "temp uploaded image path"
// done
//when the mail form is submitted
//loop through session array
//move the user uploaded/approved images to permanent folder
//save image information inside DB
}
here is the function that cause problem I wanna move the picture from temp folder to permanent location but the php_move_uploaded_file() doesn't work in my case I don't really know what is the problem please help me if you know what is the problem thnks .
function saveAdv()
{
$advTitle = $_POST['advTitle'];
$advContent = $_POST['advContent'];
if (!empty($advTitle) && !empty($advContent)) {
if (DataValidation::isOnlyPersianOrEnglish($advTitle) &&
DataValidation::isOnlyPersianOrEnglish($advContent)) {
DBconnection::insertRow('ADVERTISEMENT', ['title', 'Advertisement', 'advDate'],
[$advTitle, $advContent, date('y/m/d h:i:s')]);
// AjaxHelper::sendAjaxResponse("success");
$projectTemp = $_SESSION['ADVERTISEMENT']['Img'];
move_uploaded_file(
$projectTemp,
DOC_ROOT . "/images/advertisementImg/"
);
AjaxHelper::sendAjaxResponse($projectTemp);
}
} else {
AjaxHelper::sendErrorMessage(AjaxHelper::EMPTY_EMAIL_OR_PASSWORD);
}
}
I don't get any error I've already debuged many times but no warning and no errors at all and the location of the folders are completely correct and also there is no permission problems.
The move_uploaded_file() works pretty well at first step that I move image from system temp location to my project temp location, but doesn't work when I wanna move the image from project temp location to permanent location.
move_uploaded_file() is only for moving files which have just been uploaded in a POST request and are stored in the system temp location. As the documentation (https://php.net/manual/en/function.move-uploaded-file.php) states, it first checks whether the file is a valid upload file meaning that it was uploaded via PHP's HTTP POST upload mechanism (that's a direct quote from the docs). If it's not valid by that definition, it fails.
So, if you're trying to use move_uploaded_file() to copy files from other locations (not the system temp location) which have not been directly uploaded to that location in the current request, then it won't work. Use PHP's general file manipulation functionality for moving other files around, using the rename() function (see https://www.php.net/manual/en/function.rename.php for details).

Cakephp 3: Reading a file in the img folder from another file contained in the same folder

I'm creating an image using the gd library.
The script creates the image object, then it opens the logo.png image and paste it into $image.
$image = ImageCreate(192,64);
$myFile = imagecreatefrompng('logo.png');
imagecopy($image,$myFile,0,0,0,0,64,64);
ImageDestroy($myFile);
This script works fine in another application (with no framework), but here in CakePHP 3, it doesn't work.
The script and the logo.png image are located in the webroot/img folder of CakePHP.
The script is called this way from the view (template):
<img src="/cake/img/kana/makelogo.php">
The script crashes with no error message. (Firefox says that the image file can not be displayed because it is corrupted.)
It crashes when the line $myFile = imagecreatefrompng('logo.png'); is not commented.
Is there special routing settings to allow a script in the img folder to access a file in the img folder?
The image path is relative to the file the code is contained in. If your script is in weboroot/img/kana but the image is up a directory in webroot/img, it won't be able to find it.
You could specify a relative path instead:
$myFile = imagecreatefrompng('../logo.png');
And just FYI if it's ever moved to a CakePHP Controller, use the constant defined in config/paths.php:
$image = ImageCreate(192,64);
$myFile = imagecreatefrompng( WWW_ROOT.'/img/bricks.png');
imagecopy($image,$myFile,0,0,0,0,64,64);
ImageDestroy($myFile);
#ahoffner: It works if I do it in the controller. Here's the code I added to the controller:
function displaylogo() {
$image = ImageCreate(192,64);
$myFile = imagecreatefrompng(WWW_ROOT.'img/logo.png');
imagecopy($image,$myFile,0,0,0,0,64,64);
ImageDestroy($myFile);
ImagePng ($image);
$this->autoRender = false;
$this->response->header('Content-Type: image/png');
$this->response->type('png');
$this->response->body($image);
}
And in the view:
<img src="/cake/<MyController>/displaylogo">
This solution works for me.

FileManager For TinyMCE not getting variable

I am using TinyMCE as a WYSIWYG editor.
It is working perfectly, except for the image upload directory. I want each user to have their own directory in the images directory, but I cannot get it to work.
I am passing the user id in the URL and have tried adding the code to get it from the URL in the config.php file where the directories are defined, but the $user_id value remains empty.
Any assistance would be great.
The URL:
http://mydomain.co.za/index.php?user_id=1
The Code:
<?php
$user_id= htmlspecialchars($_GET["user_id"]);
// The URL that points to the upload folder on your site.
// Can be a relative or full URL (include the protocol and domain)
$imageURL = 'http://mydomain.co.za/images/'.$user_id;
// Full upload system path. Make sure you have write permissions to this folder
$uploadPath = '/home/username/public_html/editor/images/'.$user_id;
//We create the directory if it does not exist - you can remove this if you consider it a security risk
if(!is_dir($uploadPath)) {
mkdir($uploadPath,0755,true);
}
//Create thumb directory if doesn't exist
if(!is_dir($uploadPath . 'thumbnail')) {
mkdir($uploadPath . 'thumbnail',0755,true);
}
//Allowed extenstions
$allowedExtensions = array('jpg','gif','jpeg','bmp','tif','png');
//Maximum upload limit
$sizeLimit = 2 * 1024 * 1024;
function isAuth() {
//Perform your own authorization to make sure user is allowed to upload
return true;
}
Is it possible the reason is because it is not in the main php file?
Or Can I get the variable from the URL?
They suggested on their Instructions that I add $userId = Auth::getId(); but id returns an empty value. Plus I have no idea what that command is executing.
PLEASE NOTE:
the file management is being done by TinyMCE Image Uploader & Manager
UPDATE:
By adding the $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; echo $actual_link; I noticed by the time the $_GET command is rung the URL has changed to http://mydomain.co.za/tinymce/plugins/lioniteimages/connector/php/gallery.php, but in the browser URL bar, the URL is still the same with the variable.
Is there anyway to access that URL instead of the one i am getting?
I found the solution.
Simple enough, just created a session and the problem was solved.
I was able to get the variable from the session.

Create Thumbnail with SWFUpload

I am attempting a CMS photo gallery and need to create thumbnails at the end of my upload.php file. I have a function called create_square_image that works fine when run on the 'showphotos.php' page. However I don't want it to be run every time someone views the page. I have tried adding the following to SWFUpload...
// Process the file
/*
At this point we are ready to process the valid file. This sample code shows how to save the file. Other tasks
could be done such as creating an entry in a database or generating a thumbnail.
Depending on your server OS and needs you may need to set the Security Permissions on the file after it has
been saved.
*/
$thumb_save = "uploads/thumbs/thumb_" . $_FILES[$upload_name]["tmp_name"];
create_square_image($_FILES[$upload_name]["tmp_name"],$thumb_save,100);
if (!#move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) {
HandleError("File could not be saved.");
exit(0);
}
exit(0);
The create_square_image function uses this format:
create_square_image ( string $filename , string $destination, size )
I have tried it a few times and it will save the image but not the thumbnail.
Set the permissions on /uploads/thumbs/ to 777. You could also try specifying the absolute path to that location. To see your absolute path use this code: echo getcwd();

Saving Images to folder | PHP

I want to be able to open the provided URL (which is done via a form) that is an URL that will allow the server to save the file into a directory, for example:
http://www.google.co.uk/intl/en_com/images/srpr/logo1w.png
I want to save that logo into this directory:
img/logos/
Then it will add it to the database by giving it a random file name before so, e.g.
827489734.png
It will now be inserted to the database with the following:
img/logos/827489734.png
I do not want to use cURL for this, I like to work with fopen, file_get_contents, etc...
Cheers.
EDIT
$logo = safeInput($_POST['logo']);
if(filter_var($avatar, FILTER_VALIDATE_URL))
{
$get_logo = file_get_contents($logo);
$logo_directory = 'img/logos/';
$save_logo = file_put_contents($logo_directory, $logo);
if($save_logo)
{
$logo_path = $logo_directory . $save_logo;
A part of this code I need helping...
You need to specify a full file name when doing a file_put_contents(). A pure directory name won't cut it.

Categories