save xml file to different directory - php

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.

Related

File full path location using php

i am creating file upload script and saving the file path to the sql, i use this script
$location = 'files/' . $file_name;
$full_path = realpath($location);
It return
D:xampphtdocsau
why theres no slash, am i doing it wrong

Getting a Storage file and copying to another storage place

So, I have storage files saved and can access them like this:
$file = Storage::disk('public')->url("featured/{$i->image}")
That nets me the full url of the file I have in my storage/ directory. How can I make it so that can copy and paste that same exact file into a new directory but with a new file name?
I have tried:
$url = Storage::disk('public')->url("featured/{$i->image}");
$fileName = 'u_' . (new \DateTime())->getTimestamp() . '.png';
$fileDestination = '/new_folder/' . $fileName;
$i->image = $fileDestination;
File::copy($url, $fileDestination);
I am getting PHP execution timeout errors. I don't think I am referencing the paths correctly.
I want to copy /storage/app/public/featured/imageOne.png to /storage/app/public/new_folder/imageOneWITHNEWFILENAME.png
Is there a Storage facade solution?
Use Storage facade instead of File::copy to copy file to the new destination like
Storage::disk('public')->copy("featured/{$i->image}", $fileDestination);
Reference: Laravel 5.5 file storage

file put contents how to set a path

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.

PHP echo file size located in pc

Ι have somewhere located in my pc a txt file, let's say for example C:\Users\my_file.txt. Is there a way to print its size? As I search it, I am given:
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
But what path should I give to filename variable?
In Linux, the path separator is /. In Windows, it is either \, / or \\.
I personally use front-slashes / on both OS so I never get confused.
Below you can find 3 valid full paths on Windows, i.e.:
C:/Users/my_file.txt
or
C:\\Users\\my_file.txt
or simply:
C:\Users\my_file.txt
To get the filesize you can use the function filesize
$myFile = "C:\Users\my_file.txt"
echo filesize($myFile) . "bytes";
If the file is on the same dir as the php script you can use a relative path:
$myFile = "my_file.txt"
echo filesize($myFile) . "bytes";
If the file is one dir below you can also use a relative path.
$myFile = "../my_file.txt"
echo filesize($myFile) . "bytes";
Another relative path example, this time 1 dir UP:
$myFile = "./newdir/my_file.txt"
echo filesize($myFile) . "bytes";
Ideally you just give filesize an absolute path.
If that is not an option, you have to give it a relative path (relative to the PHP-file you are in).
Say you have a dir files with another folder in it (php), and in it again there is a file called filesize.php. In the files folder you have a file called bigfile.txt.
To get the filesize of the bigfile.txt in your php script which is in php/filesize.php you could do one of the following:
<?php
$filesize = filesize("../bigfile.txt"); // One folder up (relative)
$filesize = filesize("C:\files\bigfile.txt"); // Absolute path

password protect directory but still allow use on main page

I'm trying to move an image to a tmp directory on the server side and then send it to the client from the tmp folder, but things aren't quite working. Here's what I currently have.
//this is where the image resides permanently
$imgdirectory = "../" . $ms . "msimages/" . $img_filename;
//here I'm trying to get the contents of the img from its permanent location
$content = file_get_contents($imgdirectory);
//here I'm trying to create a new temporary file
file_put_contents('/tmp/img.jpg', $content);
...
echo "img {background:url(\"/tmp/img.jpg\")-" . $xleft . "px -" . $ytop . "px";}"; this is the css trying to use file that is supposed to be in the temp folder
and here's the css output I get when the page loads
img#{width:631px;height:453px;background:url("/tmp/img.jpg")-144px -112px;}
But unfortunately, there is no file at /tmp/img.jpg so something must not quite be working with the file_put_contents 404 image not found
btw, I'm not getting any errors about permissions to put contents in /tmp,
Any advice is appreciated.
Use copy instead of reading and writing files
bool copy ( string $source , string $dest [, resource $context ] )
And
file_put_contents('/tmp/img.jpg', $content);
will put it under root tmp directory not in your sites tmp directory.
Try
file_put_contents($_SERVER["DOCUMENT_ROOT"] . 'tmp/img.jpg', $content);

Categories