I have a simple open (file) method which should throw an exception if it fails to open or create a file in the given path:
const ERR_MSG_OPEN_FILE = 'Failed to open or create file at %s';
public function open($filePath)
{
ini_set('auto_detect_line_endings', TRUE);
if (false !== ($this->handler = #fopen($filePath, 'c+'))) {
return true;
} else {
throw new \Exception(
sprintf(self::ERR_MSG_OPEN_FILE, $filePath)
);
}
}
There is unit-test around it using PHPUnit and VFSStream:
$structure = [
'sample.csv' => file_get_contents(__DIR__ . '/../sampleFiles/small.csv')
];
$this->root = vfsStream::setup('exampleDir', null, $structure);
$existingFilePath = vfsStream::url('exampleDir') . DIRECTORY_SEPARATOR . 'sample.csv';
$this->file = new File();
$this->file->open($existingFilePath);
The above setup creates a virtual directory structor containing a mock file (read/cloned from an existing CSV file) and this satisfy the open method's requirement to open a file. And also if I pass a different path (none existing file) it will be created in the same virtual structor.
Now in order to cover the exception I want to add a ready-only directory to the existing structor, lets say a folder belong to another user and I don't have write permission on it, like this when I try to open a non existing file in that folder, attempt to creating one should fail and the open method should throw the exception.
The only problem is,.... I don't know how to do it :D
Any Advice, hint and guidance will be appreciated :)
You could simply point your mock directory's url to an incorrect path.
$existingFilePath = vfsStream::url('badExampleDir') . DIRECTORY_SEPARATOR . 'incorrect.csv';
considering the badExampleDir never been setup your method will fail to create the file in it.
Related
i created an Artisan Command that created Repository. A Repository File and an Interface.
when user run below command, this files will be generated.
php artisan make:repository RepositoryName
now, next step is to add bind code to RepositoryServiceProvider in register method.
How do I add the following code to that file?
$this->app->bind(
RepositoryNameInterface::class,
RepositoryName::class,
);
and generally, how to add custom code to the method of class in PHP?
You can keep the repositories interface as key and repository as the value in an array in a file.
First, add use Illuminate\Filesystem\Filesystem; and initialize it in the make:repository command constructor like :
protected $files;
public function __construct( Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
Let's add a function in your make:repository command.
// Add a function in make:repository command
public function writeToRepoCacheFile($repoName, $repoInterface)
{
// Let's make sure cache folder exists
$cacheDirPath = base_path('bootstrap') . DIRECTORY_SEPARATOR . 'cache';
$this->files->ensureDirectoryExists($cacheDirPath);
// The full path we will be keeping the array of repos
$file = $cacheDirPath . DIRECTORY_SEPARATOR . 'repositories.php';
if (! is_writable(dirname($file))) {
throw new Exception('The '.dirname($file).' directory must be present and writable.');
}
// Merge with the previously added repo's if available
$repositories = [];
if ($this->files->exists($file)) {
$repositories = $this->files->getRequire($file);
}
$repositoryList = array_merge($repositories, [
$repoInterface => $repoName
]);
$this->files->replace($file, '<?php'. PHP_EOL . PHP_EOL .'return ' . var_export($repositoryList, true).';');
}
Now, whenever you create a new repo, the writeToRepoCacheFile method should be called with parameters of $repoName and $interface with their respective path. The method will simply create a cache file in our bootstrap/cache folder naming repositories and add your recently added repos as an array in the file which later will be called in our service provider to get register.
The final one is to bind the interface with our repository.
public function bindTheRepos()
{
$repositories = base_path('bootstrap/cache/repositories.php');
if ($this->files->exists($repositories)) {
Collection::make($this->files->getRequire($repositories))
->map(function ($repo, $repoInterface) {
// Make sure the files exists
// Interface exists
$interfaceExists = $this->app['files']->exists(
$repoInterface.'.php'
);
// Repo exists
$repoExists = $this->app['files']->exists(
$repo.'.php'
);
if (
$interfaceExists &&
$repoExists
))) {
$this->app->bind($repoInterface, $repo);
}
});
}
}
Add the above function in your service provider and call the function in the register method of the service provider.
Make sure the interface and repository classes path matches your structured path.
This will dynamically bind your newly created repository with its interface.
Im building an application where I need to dynamically create some directories using the Azure's PHP SDK.
I did it using a loop but Im unsure if thats the correct way of doing it so heres my code;
I cant create a path that already exists so I have to check level by level if a directory and exists, than enters it and repeat.
public function generateDirectory($path)
{
$pathArray = explode("/", $path);
$currentPath = "";
try {
foreach ($pathArray as $key => $slice) {
$directories = $this->fileClient->listDirectoriesAndFiles("abraco", $currentPath)->getDirectories();
$currentPath .= $slice . "/";
$exists = false;
foreach ($directories as $key => $directory) {
if ($directory->getName() === $slice) {
$exists = true;
break;
}
}
if (!$exists) {
$this->fileClient->createDirectory("abraco", $currentPath);
}
}
return true;
} catch (Exception $e) {
return false;
}
}
Doesnt it should have a method to create a full path with subfolders? I think that this way is not performatic.
Doesnt it should have a method to create a full path with subfolders? I think that this way is not performatic.
I agree with you that there is a method to create a full path with subfolders will be better.
But currently, as you metioned that if we want to create full path with subfolders, we need to create the directory folder level by level.
If you use fiddler to capture request while you create multi-level directory structure via PHP SDK,you could find it use the following Rest API
https://myaccount.file.core.windows.net/myshare/myparentdirectorypath/mydirectory?
restype=directory
For more information please refer to Azure file Storage Create directory API.
myparentdirectorypath Optional. The path to the parent directory where mydirectory is to be created. If the parent directory path is omitted, the directory will be created within the specified share.
If specified, the parent directory must already exist within the share before mydirectory can be created.
I have a AWS EC2 server with phpMyAdmin to manage it.
Everything is working correctly but I would like to be able to create another folder in the /var/www/html directory to add files..
This is my code but it just keeps returning the error to me! any ideas??
// STEP 2.2 Create a folder in server to store posts'pictures
$folder = "/var/www/html/bloggerFiles/Posts/" . $id;
if(!file_exists($folder)){
if (!mkdir($folder, 0777, true)) {//0777
die('Failed to create folders...');
}
}
I would normally create that folder in the terminal by using sudo mkdir, but when I add sudo Nothing works!
Any help is appreciated!
Thanks in advance.
Make sure the folder(s) you are accessing are set to read and write folder permissions, then use this function:
function newFolder($path, $perms)
$path = str_replace(' ', '-', $path);
$oldumask = umask(0);
mkdir($path, $perms); // or even 01777 so you get the sticky bit set (0777)
umask($oldumask);
return true;
}
This fixed it for me.
You can create new folder doing this: newFolder('PathToFolder/here', 0777);
EDIT: Please have a look at: https://www.youtube.com/watch?v=7mx2XOFBp8M
EDIT: Also have a look at http://php.net/manual/en/function.mkdir.php#1207
EDIT: Storing functions in classes and safely use the function
class name_here
{
public function newFolder($path, $perms, $deny_if_folder_exists){
$path = 'PATH_TO_POSTS/'.$path; // This is for setting the root to PATH TO POSTS
$path = str_replace('../', '', $path); // Deny the path to go out of var/www/html/PATH_TO_POSTS/$path
if( $deny_if_folder_exists === true ){
if(file_exists($path)){return false;}
$old_umask = umask(0);
mkdir($path, $perms);
umask($old_umask);
}elseif( $deny_if_folder_exists === false ){
$old_umask = umask(0);
mkdir($path, $perms);
umask($old_umask);
}else{
return false; // Unknown
}
}
}
/* Call the function by doing this: */
$manage = new name_here;
$manage->newFolder('test', 777, true); // Test will appear in /var/www/html/PATH_TO_POSTS/$path, but if the folder exists it will return false and not create the folder.
EDIT: If this file is called from html it will re create the path, so I will it has to be called from /html/
EDIT: How to use the name_here class
/*
How to call the function?
$manage = new name_here; Creates a variable to an object (The class)
$manage->newFolder('FolderName', 0777, true); // Will create a folder to the path,
but this fill needs to be called from the html the root directory is set to the
"PATH_TO_POSTS/" basicly means you cannot do this function from "html/somewhere/form.php",
UNLESS the "PATH_TO_POSTS" is in the same directory as form.php
*/
I have uploaded a lot of images from the website, and need to organize files in a better way.
Therefore, I decide to create a folder by months.
$month = date('Yd')
file_put_contents("upload/promotions/".$month."/".$image, $contents_data);
after I tried this one, I get error result.
Message: file_put_contents(upload/promotions/201211/ang232.png): failed to open stream: No such file or directory
If I tried to put only file in exist folder, it worked. However, it failed to create a new folder.
Is there a way to solve this problem?
file_put_contents() does not create the directory structure. Only the file.
You will need to add logic to your script to test if the month directory exists. If not, use mkdir() first.
if (!is_dir('upload/promotions/' . $month)) {
// dir doesn't exist, make it
mkdir('upload/promotions/' . $month);
}
file_put_contents('upload/promotions/' . $month . '/' . $image, $contents_data);
Update: mkdir() accepts a third parameter of $recursive which will create any missing directory structure. Might be useful if you need to create multiple directories.
Example with recursive and directory permissions set to 777:
mkdir('upload/promotions/' . $month, 0777, true);
modification of above answer to make it a bit more generic, (automatically detects and creates folder from arbitrary filename on system slashes)
ps previous answer is awesome
/**
* create file with content, and create folder structure if doesn't exist
* #param String $filepath
* #param String $message
*/
function forceFilePutContents ($filepath, $message){
try {
$isInFolder = preg_match("/^(.*)\/([^\/]+)$/", $filepath, $filepathMatches);
if($isInFolder) {
$folderName = $filepathMatches[1];
$fileName = $filepathMatches[2];
if (!is_dir($folderName)) {
mkdir($folderName, 0777, true);
}
}
file_put_contents($filepath, $message);
} catch (Exception $e) {
echo "ERR: error writing '$message' to '$filepath', ". $e->getMessage();
}
}
i have Been Working on the laravel Project With the Crud Generator and this Method is not Working
#aqm so i have created my own function
PHP Way
function forceFilePutContents (string $fullPathWithFileName, string $fileContents)
{
$exploded = explode(DIRECTORY_SEPARATOR,$fullPathWithFileName);
array_pop($exploded);
$directoryPathOnly = implode(DIRECTORY_SEPARATOR,$exploded);
if (!file_exists($directoryPathOnly))
{
mkdir($directoryPathOnly,0775,true);
}
file_put_contents($fullPathWithFileName, $fileContents);
}
LARAVEL WAY
Don't forget to add at top of the file
use Illuminate\Support\Facades\File;
function forceFilePutContents (string $fullPathWithFileName, string $fileContents)
{
$exploded = explode(DIRECTORY_SEPARATOR,$fullPathWithFileName);
array_pop($exploded);
$directoryPathOnly = implode(DIRECTORY_SEPARATOR,$exploded);
if (!File::exists($directoryPathOnly))
{
File::makeDirectory($directoryPathOnly,0775,true,false);
}
File::put($fullPathWithFileName,$fileContents);
}
I created an simpler answer from #Manojkiran.A and #Savageman. This function can be used as drop-in replacement for file_put_contents. It doesn't support context parameter but I think should be enough for most cases. I hope this helps some people. Happy coding! :)
function force_file_put_contents (string $pathWithFileName, mixed $data, int $flags = 0) {
$dirPathOnly = dirname($pathWithFileName);
if (!file_exists($dirPathOnly)) {
mkdir($dirPathOnly, 0775, true); // folder permission 0775
}
file_put_contents($pathWithFileName, $data, $flags);
}
Easy Laravel solution:
use Illuminate\Support\Facades\File;
// If the directory does not exist, it will be create
// Works recursively, with unlimited number of subdirectories
File::ensureDirectoryExists('my/super/directory');
// Write file content
File::put('my/super/directory/my-file.txt', 'this is file content');
I wrote a function you might like. It is called forceDir(). It basicaly checks whether the dir you want exists. If so, it does nothing. If not, it will create the directory. A reason to use this function, instead of just mkdir, is that this function can create nexted folders as well.. For example ('upload/promotions/januari/firstHalfOfTheMonth'). Just add the path to the desired dir_path.
function forceDir($dir){
if(!is_dir($dir)){
$dir_p = explode('/',$dir);
for($a = 1 ; $a <= count($dir_p) ; $a++){
#mkdir(implode('/',array_slice($dir_p,0,$a)));
}
}
}
I am using Zend Framework 1.9.6. I think I've got it pretty much figured out except for the end. This is what I have so far:
Form:
<?php
class Default_Form_UploadFile extends Zend_Form
{
public function init()
{
$this->setAttrib('enctype', 'multipart/form-data');
$this->setMethod('post');
$description = new Zend_Form_Element_Text('description');
$description->setLabel('Description')
->setRequired(true)
->addValidator('NotEmpty');
$this->addElement($description);
$file = new Zend_Form_Element_File('file');
$file->setLabel('File to upload:')
->setRequired(true)
->addValidator('NotEmpty')
->addValidator('Count', false, 1);
$this->addElement($file);
$this->addElement('submit', 'submit', array(
'label' => 'Upload',
'ignore' => true
));
}
}
Controller:
public function uploadfileAction()
{
$form = new Default_Form_UploadFile();
$form->setAction($this->view->url());
$request = $this->getRequest();
if (!$request->isPost()) {
$this->view->form = $form;
return;
}
if (!$form->isValid($request->getPost())) {
$this->view->form = $form;
return;
}
try {
$form->file->receive();
//upload complete!
//...what now?
$location = $form->file->getFileName();
var_dump($form->file->getFileInfo());
} catch (Exception $exception) {
//error uploading file
$this->view->form = $form;
}
}
Now what do I do with the file? It has been uploaded to my /tmp directory by default. Obviously that's not where I want to keep it. I want users of my application to be able to download it. So, I'm thinking that means I need to move the uploaded file to the public directory of my application and store the file name in the database so I can display it as a url.
Or set this as the upload directory in the first place (though I was running into errors while trying to do that earlier).
Have you worked with uploaded files before? What is the next step I should take?
Solution:
I decided to put the uploaded files into data/uploads (which is a sym link to a directory outside of my application, in order to make it accessible to all versions of my application).
# /public/index.php
# Define path to uploads directory
defined('APPLICATION_UPLOADS_DIR')
|| define('APPLICATION_UPLOADS_DIR', realpath(dirname(__FILE__) . '/../data/uploads'));
# /application/forms/UploadFile.php
# Set the file destination on the element in the form
$file = new Zend_Form_Element_File('file');
$file->setDestination(APPLICATION_UPLOADS_DIR);
# /application/controllers/MyController.php
# After the form has been validated...
# Rename the file to something unique so it cannot be overwritten with a file of the same name
$originalFilename = pathinfo($form->file->getFileName());
$newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension'];
$form->file->addFilter('Rename', $newFilename);
try {
$form->file->receive();
//upload complete!
# Save a display filename (the original) and the actual filename, so it can be retrieved later
$file = new Default_Model_File();
$file->setDisplayFilename($originalFilename['basename'])
->setActualFilename($newFilename)
->setMimeType($form->file->getMimeType())
->setDescription($form->description->getValue());
$file->save();
} catch (Exception $e) {
//error
}
By default, files are uploaded to the system temporary directory, which means you'll to either :
use move_uploaded_file to move the files somewhere else,
or configure the directory to which Zend Framework should move the files ; your form element should have a setDestination method that can be used for that.
For the second point, there is an example in the manual :
$element = new Zend_Form_Element_File('foo');
$element->setLabel('Upload an image:')
->setDestination('/var/www/upload')
->setValueDisabled(true);
(But read that page : there are other usefull informations)
If you were to move the file to a public directory, anyone would be able to send a link to that file to anyone else and you have no control over who has access to the file.
Instead, you could store the file in the DB as a longblob and then use the Zend Framework to provide users access the file through a controller/action. This would let you wrap your own authentication and user permission logic around access to the files.
You'll need to get the file from the /tmp directory in order to save it to the db:
// I think you get the file name and path like this:
$data = $form->getValues(); // this makes it so you don't have to call receive()
$fileName = $data->file->tmp_name; // includes path
$file = file_get_contents($fileName);
// now save it to the database. you can get the mime type and other
// data about the file from $data->file. Debug or dump $data to see
// what else is in there
Your action in the controller for viewing would have your authorization logic and then load the row from the db:
// is user allowed to continue?
if (!AuthenticationUtil::isAllowed()) {
$this->_redirect("/error");
}
// load from db
$fileRow = FileUtil::getFileFromDb($id); // don't know what your db implementation is
$this->view->fileName = $fileRow->name;
$this->view->fileNameSuffix = $fileRow->suffix;
$this->view->fileMimeType = $fileRow->mime_type;
$this->view->file = $fileRow->file;
Then in the view:
<?php
header("Content-Disposition: attachment; filename=".$this->fileName.".".$this->fileNameSuffix);
header('Content-type: ".$this->fileMimeType."');
echo $this->file;
?>
$this->setAction('/example/upload')->setEnctype('multipart/form-data');
$photo = new Zend_Form_Element_File('photo');
$photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../public/tmp/upload");
$this->addElement($photo);