Silverstripe 3.1 - mkdir() issue bug or local issue? - php

I seem to get this error on my local server when the site is loaded for the first time e.g. in the morning. Once I do a refresh it's gone...
I'm using silverstripe 3.1.
Is there a way to prevent this locally or is this a bug?
Warning: mkdir(): File exists in /framework/core/manifest/ManifestCache.php on line 19
Looks like line 19 is trying to create a TEMP folder but it already exists...
function __construct($name) {
$this->folder = TEMP_FOLDER.'/'.$name;
if (!is_dir($this->folder)) mkdir($this->folder);
}
Should that function check if the folder exists first e.g.
if (!is_dir($this->folder) || !file_exists($this->folder)) mkdir($this->folder);

Seems that there exists a file with the same name as the directory. That's why is_dir() returns false but mkdir() fails because the file exists.
You can change this to:
if (!file_exists($this->folder)) mkdir($this->folder);
This should work so far.
However it is necessary to mention that such file existence tests are vulnerable against race conditions by design. That's why you need to additionally check the return value of mkdir():
if (!file_exists($this->folder)) {
if(#mkdir($this->folder) === FALSE) {
throw new Exception('failed to create ' . $this->folder);
}
}
This may not being required if you (or the framework) has registered a global error handler which turns warning into exceptions, because mkdir() will throw a warning on errors.

Related

PHP and Creating Directories

Quick question. I'll start off with an example.
I have a social site similar to Facebook. When a user signs up my PHP script creates a directory named after his username and a custom sub-directory for this user called "photos".
The "photos" directory is never created when someone signs up. I assume it is because the actual server hasn't created the actual username directory in the first place.
My solution is that anytime i need to execute the photo function on my site for a user I should check if the folder exists and if not create it, which works fine. Is this standard procedure? Or is there a better way to create multiple directories at the same time while running checks.
When someone signs up I run this code, and the latter if statement doesn't successfully create the photos folder, so i check these two statements anytime I am editing something that would go in photos as a check for if the folder exists.
if (!file_exists("user/$username")) {
mkdir("user/$username", 0755);
}
if (!file_exists("user/$username/photos"))
{
mkdir("user/$username/photos", 0755);
}
The directories are created sequentially/synchronously thus once execution of the first mkdir() is finished the folder should be there with the indicated permission set.
As stated here the mkdir()
Returns TRUE on success or FALSE on failure.
and
Emits an E_WARNING level error if the directory already exists.
Emits an E_WARNING level error if the relevant permissions prevent
creating the directory.
So you can go:
if (!file_exists("user/$username")) {
if(mkdir("user/$username", 0755))
{
//directory created
if (!file_exists("user/$u/photos"))
{
mkdir("user/$username/photos", 0755);
}
}
else
{
//directory not created
}
}
If the mkdir() returns false - check the logs or handle the error.
You can do a quick directory 'exists' check like this
function check($images_dir) {
if($handle = opendir($images_dir)) {
// directory exists do your thing
closedir($handle);
} else {
// create directory
}
}
Where $images_dir = path/directory/user/$username

Getting error after inserting 3 pages php data insert script

I have 3 php pages where details is supposed to be inserted to db in final php.. I am geetting these warnings below however data is inserted. I know these warning can be turned of by error reporting but doesnt look good to me go for that..
Warning: copy(): The first argument to copy() function cannot be a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 301
Warning: unlink(tmp_imgs/tmp_1011/..): Is a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 302
Warning: copy(): The first argument to copy() function cannot be a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 301
Warning: unlink(tmp_imgs/tmp_1011/.): Is a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 302
I think I have defined it wrong way.. seeking help..
I am getting error on this section:
// Moveing temp images to property directory
if ($handle = opendir('tmp_imgs/tmp_'.$property_id)) {
while (false !== ($file = readdir($handle)))
{
//$file_ext = strtolower(substr($file, strrpos($file, '.') + 1));
copy('tmp_imgs/tmp_'.$property_id.'/'.$file, 'property_images/img_'.$property_id.'/'.$file);
unlink('tmp_imgs/tmp_'.$property_id.'/'.$file);
}
closedir($handle);
}
The error is clear: you cannot use that function to copy directories. However, you may not be aware that you're even trying to do this in the first place.
There are magical "files" in each directory which are themselves directories (. and ..), so if you're iterating over a directory and copying everything in it, you need to explicitly skip those two.

PHP rename(<from>, <to>) gives warning: "Is a directory"

I have the following function:
function move($currentPath, $newPath)
{
if (!$this->_createFolder($newPath))
return false;
if (!rename($currentPath, $newPath))
return false;
return true;
}
where _createFolder() checks if the directory exists, and if it doesn't, creates it. I'm consistently getting the following warning:
"rename(/home/user/folder/folder/app/webroot/img/listings/incomplete/15/0/picture1.png,/home/user/folder/folder/app/webroot/img/listings/130/picture1.png): Is a directory "
The file is successfully copied to the second directory, but is not deleted from the first directory. rename() returns false and this warning is given. I thought it might be something with permissions, but after trying a bunch of things I couldn't seem to figure it out.
Any help would be appreciated.
your code is creating a folder using the $newpath
if (!$this->_createFolder($newPath))
return false;
then $newpath becomes a directory.

Check if directory is created (Wordpress and/or PHP)

I'm relatively new to PHP and I'm trying to write my own plugin. Upon plugin activation it will run the following function:
function kb_create_uploadfolder () {
global $wpdp;
$upload_dir = wp_upload_dir();
$upload_dir = $upload_dir['basedir'] . "/plugin_uploads";
$upload_dircheck = wp_mkdir_p($upload_dir);
}
I didn't bother to check whether the directory already exists before creating it since I figured it won't overwrite anything or delete the contents if it does. Correct me if I'm wrong.
The thing is however, I would like to check if the creation of the directory was succesful or not but I can't figure out how to get this information.
Use is_dir():
if(is_dir($upload_dircheck))
{
echo "It is a dir";
}
else
{
echo "Sorry, non-existent or not a dir";
}
Also, mkdir() doesn't delete or overwrite existing contents, it just creates a directory if it does not yet exist.
If you're using PHP 4 or newer then you can use the is_dir() function.
Try is_dir().

PHP - Failed to open stream: no such file or directory - for existing file

I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.
Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.
The error:
Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301
The code:
if (file_exists($filename)) {
$image = imagecreatefrompng($filename);
}
I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.
I suggest you use is_readable
if (is_readable($filename)) {
$image = imagecreatefrompng($filename);
}
The file may "exist" but is the file accessible? what does file_exists actually do?
if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.
it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.
maybe try is_readable() or is_writable() instead?
Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.
Use is_readable() to check whatever you have permission to access that file.
You can try GD :
IF($img = #GETIMAGESIZE("testimage.gif")){
ECHO "image exists";
}ELSE{
ECHO "image does not exist";
}
bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.

Categories