password protect directory but still allow use on main page - php

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);

Related

is it possible to send a zipped folder from server (php) to front end (angular) ? or is there another way to do it

I am using drupal as a back end.
in drupal I get some .pdf files. and then zip them using drupal zip archive. then saving this archive in my server tmp folder, i get the tmp folder using php sys_get_temp_dir()
now...
what should i return to the front end (Angular) so that the user can download this folder..
this is an example code i used for zipping:
$nodesIds = [1024, 1023, 1022]; // those are just some example ids, i get the real ids from the front end post request.
$zipName = $this->generateUniqueName();
$zip = new \ZipArchive;if(!$zip->open(sys_get_temp_dir() . '/' . $zipName, constant("ZipArchive::CREATE"))) {
return new JsonResponse('could not open zip');
}
foreach ($nodesIds as $id) {
$node = \Drupal\node\Entity\Node::load($id);
$uri = $node->filed_file->entity->getFileUri();
$name = $node->field_file->entity->id() . '_'. $node->field_file->entity->getFilename();
$url = $file_system->realpath($uri);
$zip->addFile($url, $name);
}
$zip->close();
i tried returning a link to the zipped folder in the server:
return new JsonResponse(sys_get_temp_dir() . '/' . $zipName);
but i dont know what to do with that from angular.
Also tried to return a stream using symfony:
$stream = new Stream(sys_get_temp_dir() . '/' . $zipName);
$response = new BinaryFileResponse($stream);
a stream and not a file because the user chooses which files to zip.. so they can choose as much as they want.. it might get to even a 100 .pdf files ..
every thing works fine and i get the zipped folder in tmp.. but now what ?
I want to download this zipped folder to the user browser..
but I do not know how!. should I return the zipped folder, then in angular use it as a blob or somehow deal with it and serve it to the user,,
or maybe the right way is to send back the link to its location in the tmp folder,, and in angular i only access that location and somehow get the folder (i dont know if this is even possible due to permissions and security), or is there another better way that I do not know about.
thank you very much.

wordpress remove uploaded file

I have created a simple script to upload file in my WordPress plugin using
wp_handle_upload
In database only link to this image is stored. I would like to delete this uploaded file when i delete the post which it is linked to, however using
unlink()
does not work due to link structure which looks like this:
http://localhost/wp-content/uploads/2016/10/image.jpg
Does Anyone know the way to remove "http://[ip]/" from path or any WordPress method to remove uploaded file
I would be grateful for help.
You can use get_home_path() to get the root directory. Then your code would be:
$url = 'http://localhost/wp-content/uploads/2016/10/image.jpg';
$path = parse_url($url, PHP_URL_PATH); // Remove "http://localhost"
$fullPath = get_home_path() . $path;
unlink($fullPath);

save xml file to different directory

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.

read all files in a directory php

Good day every one
I have a problem.
I have a folder in my webroot/files that has some documents in it, mainly doc files.
I want to read all the contents of the files. e.g. if there are 2 files, namely users.docx and funds.docx.
I want to be able to open each file, read its content, and write them to a single document.
so far the code i have writes only the 1st file, but the newly written file has a size of both the read files.
function writeToFile($insId,$path,$hospital){
$data = '';
$my_file = WWW_ROOT . 'files' . DS . 'instructions' . DS . $insId . DS ."Final Report ".$insId.".rtf";
$handle = fopen($my_file, 'w')or die('Cannot open file: '.$my_file); //implicitly creates file
foreach($hospital as $h){
$data .= file_get_contents($path.'/'.$h, false);
$data .= "\n";
echo($h."\n");
}
file_put_contents($my_file, $data);
fclose($handle);
}
A docx file is actually a directory so you will need to open that if you want to actually get to the xml that makes up the file. However, the contents of the directory is pretty complex and consists of many different files.
It's very unlikely that you will be able to find the right parts of each file and combine them together to make a valid file afterwards with a script.

Is there a way to use file_put_contents() and store the cached contents in a different directory?

I am able to put the file contents if I stay in the same directory level
ie: `file_put_contents('cache.txt', $result);
But how would I put the contents up a folder, or even two? Adding '../cache/cache.txt' does not seem to work.`
That would work fine assuming the user the web server runs as has write permission to that directory.
Try using the direct path of where you would like to put the cache content.
<?php
$file = 'people.txt';
$cacheFile = __FILE__ . '/cache/'. $file;
$content = 'Something';
file_put_contents($file, $content);
file_put_contents($cacheFile, $content);
?>
Assuming you have a folder called cache, with write permissions to it.

Categories