I am using file put contents and set a file name but file saved on the directory where the script was written. How to do to set the full path to save to disc or desktop. Something like browse window and save path.
$fileName = 'etykieta_' . $package_no . '.' . $xml->label->format;
file_put_contents($fileName, base64_decode($xml->label->base64));
And i want to set a file path in a window.
Related
I want to clear .txt file contents; the file name is stored in the variable $data['soft_files'] which is taken by a button and sent to the controller from javascript.
$data['soft_files'] = $this->input->post('soft_files');
$file = FCPATH . "/software_files/$this->input->post('soft_files')";
file_put_contents ($file, "");
I have a form with a file to uplaod. All works find. But I don't want to move the file directly into a folder.
After submit I show a confirm page and there I show the uploaded file with
header('Content-Type: image/x-png');
$file = file_get_contents(\Illuminate\Support\Facades\Input::file('restImg'));
$imgType = \Illuminate\Support\Facades\Input::file('restImg')->guessClientExtension();
echo sprintf('<img src="data:image/png;base64,%s" style="max-height: 200px"/>', base64_encode($file));
This works fine. After the confirmation I like to move the file to a folder. How can I move the file after the confirmation? The Input::get('file') is not available anymore.
You will have to store the file in the initial upload somewhere temporarily other than the default tmp directory.
The documentation for PHP file uploads says:
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed
This means that moving onto the next request, the file will no longer be available.
Instead, move it to your own custom temp directory or rename it to something special, then keep the filename in the $_SESSION to persist it to the next request.
For Laravel, this should mean putting it in the /storage directory with something like this:
// Get the uploaded file
$file = app('request')->file('myfile');
// Build the new destination
$destination = storage_path() . DIRECTORY_SEPARATOR . 'myfolder';
// Make a semi-random file name to try to avoid conflicts (you can tweak this)
$extension = $file->getClientOriginalExtension();
$newFilename = md5($file->getClientOriginalName() . microtime()).'.'.$extension;
// Move the tmp file to new destination
app('request')->file('myfile')->move($destination, $newFilename);
// Remember the last uploaded file path at new destination
app('session')->put('uploaded_file', $destination.DIRECTORY_SEPARATOR.$newFilename);
Just remember to unlink() the file after the second request or do something else with it, or that folder will fill up fast.
Additional Reference:
http://api.symfony.com/2.7/Symfony/Component/HttpFoundation/File/UploadedFile.html
I have a form on my app that allows users to upload a file .. That file gets given a random name, saved to the file system and the details including the original name of the file and the random name of the file gets saved to the database.
When the user clicks the link to re-download the file, i want the file to be renamed to the original file name on the fly but kept as the random name in the file system... if that makes sense.
The code i'm using to download the file at the moment is as follows
// Download the document
$file = public_path() . '/uploads/' . $document->userid . '/' . $document->storedname;
return Response::download($file);
The random filename that is created on upload is $document->storedname
The original name of the file is stored as $document->originalname
Pass it as the second argument to the download method:
return Response::download($file, $document->originalname);
$newName = "download.pdf";
$filename="samplepdf.pdf";
$path = storage_path("app/public/" . $filename);
if (!\File::exists($path)) {
return response()->json(['status' => false], 404);
}
return response()->download($path,$newName);
I am looking to save an xml file to a different directory from the root using php5. any ideas?
//write to the file
$filename = $id . ".xml";
$doc->save($filename);
I want to save the file to the /xml/ directory.
Change the argument to $doc->save to include the path
$filename = '/xml/' . $id . ".xml";
$doc->save($filename);
Now the thing to bear in mind is that this is a filesystem path, not web URL so its literally going to save in /xml not DOCUMENT_ROOT/xml.
I used a file upload form to upload files to my server. Then I echo $_FILES["file"]["tmp_name"] but it returns to me a location that does not seem to exist. eg /tmp/phpBparj4 is the output but there is no such file/folder in my /tmp folder. I have also checked for the appropriate permissions for the folder
My actual concern is move_uploaded_file($_FILES["file"]["tmp_name"],$target); which is not moving the uploaded file to the target location. I have also tried move_uploaded_file($_FILES["file"]["tmp_name"].$file_name,$target);
I am not sure if I understand right but why can't you specify the file folder location? i.e."
//set the right path from the server perspective
$article_file_path = $_SERVER['DOCUMENT_ROOT'];
// you should check for the file if it is/ or not already there (something like this)
if (!file_exists($article_file_path ."/your_folder/file_subfolder/". $_FILES["my_file"]["name"]))
{ do something.....
// then make your script upload the file exactly where you want it
move_uploaded_file($_FILES["my_file"]["tmp_name"],
$article_file_path ."/your_folder/file_subfolder/".$_FILES["my_file"]["name"]);
// and the download link to the uploaded file would be something like this:
echo "http://". $_SERVER["HTTP_HOST"] . "/files-article/".$_FILES["my_file"]["name"]";