How to add parent dir variable to scandir (PHP) - php

Hi sorry this is probably a really newbie question, but I'm searching for the answer for 2 days now and no luck.
I'm using a script what generates an image gallery from an uploaded folder.
Script info:
Tim-thumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks.
You need to add the directory name what contains the images to the script like:
$dirname = "images/" ;
$images = scandir($dirname);
shuffle($images);
What I'm trying to do is make the script work automatically if i upload it in a new folder with new images so I don't need to add the $dirname every time I upload a gallery.
for example:
$dirname = $mydir ;
where mydir returns the path of the current directory like:
$mydir = basename(getcwd()) . DIRECTORY_SEPARATOR ;
but it's not working.
also tried to make it work from a function:
function current_dir()
{$path = dirname($_SERVER[PHP_SELF]);$position = strrpos($path,'/') + 1;print substr($path,$position);}
than -->
$dirname = current_dir() ;
but no luck.
I think I'm missing something here, I'm a totally noob and maybe it's just a syntax issue but can't make it work.
I always get
[function.scandir]: failed to open dir ...
or creates the gallery but the images not working (I see only alt tags)
thank you for any help.
>>>>>>>>>>>>>>>>>>>
EDIT :
!!!
Just realized... :o !!!
The answer is:
$dirname = "./" ;
omg
<<<<<<<<<<<<<<<<<<<
"I think I'm missing something here..."
:D

Try
$parent = dirname(__DIR__);
http://php.net/manual/en/language.constants.predefined.php

Related

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

Cannot unlink file in Codeigniter

In my project I have a folder secure in root. The project package looks like:
application
secure
system
...........
Inside secure folder I am uploading some images on form submit using
$config1['upload_path'] = './secure/';
$ext = end(explode(".", $_FILES['thumb_image']['name']));
$config1['file_name'] = time().$_FILES['thumb_image']['name'];
$config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('thumb_image');
and it is working properly. Now while on editing the details, using another form, if I am uploading a new image instead of the current image file, I want to unlink the current one and then upload new file.
For this I am using the code:
unlink(base_url("secure/".$data['row']->videothumbnail));
I also tried with
unlink('/secure/'.$data['row']->videothumbnail);
where $data['row']->videothumbnail) is the current image file from database. New file is successfully uploaded. But old file is not getting unlinked. I have set the permission of secure folder to 777. But the images are uploaded with read only permission. Is it because of this, it is not getting unlinked?
Can anyone help me to solve this?
Thanks in advance.
Try this:
Set the permission dynamically using:
#chmod('./secure/'.$data['row']->videothumbnail, 0777);
then try unlink:
#unlink('./secure/'.$data['row']->videothumbnail);
Try echoing the path that you are providing to unlink function.
It should be something like this:
base_url()."secure/".$data['row']->videothumbnail;
I also had this issue even after setting the right permission on the folder. But the following code worked for me.
unlink(realpath(APPPATH . '../uploads').'/'.$ImageName);
Try to use $_SERVER['DOCUMENT_ROOT'] instead of base_url
$this->load->helper("file")
unlink(base_url('folder/file.ext'));
location:
\app\controller
\system\libraries
**folder\file.ext**
$unlinkUrl = "secure/".$data['row']->videothumbnail;
if(file_exists($unlinkUrl)){
unlink($unlinkUrl);
}
else{
echo $unlinkUrl." is not available";
}
I think you are just making a stupid mistake.
Firstly, the first param of unlink should be a relative path or absolute path, but base_url function will return you a path contains domain name, HOW CAN YOU DELETE A FILE ON REMOTE SERVER ?
Secondly, '/secure/'.$data['row']->videothumbnail here is not a relative path but a absolute path
YOU MUST change it into /the/absolute/path/to/secure/ or ./the/relative/path/to/secure/ (DO NOT MISS THE DOT)
use this to unlink
$oldthumb = "secure/".$data['row']->videothumbnail;
#unlink($oldthumb);
First Load the $this->load->helper("file") and then unlink it
unlink("secure/".$data['row']->videothumbnail);
if ($rowAffected > 0) {
if ($isMediaUpload)
if (file_exists('./uploads/' . $this->input->post('img_url')))
unlink('./uploads/' . $this->input->post('img_url'));
redirect('/admin/configration', 'location');
}
Though i came in late but someone might need this .
unlink(FCPATH."secure/".$data['row']->videothumbnail)
**FCPATH** - path to front controller, usually index.php
**APPPATH** - path to application folder
**BASEPATH** - path to system folder.

Rename all files in directory using PHP and creation date

i have a problem/challenge on my Synology NAS. I have a IPcam connected which takes pictures with file names like:
00A8F700CB18()_1_20140107000224_3674.jpg
Now i would like to rename all those files to something like:
Tue 07-01-2014_11-17-26.jpg (containing date & time)
And here's the kicker: I've seen (PHP) scripts using "jhead" or "stat -c", unfortunately those are not an option on the Synology!
I cooked something up which works when i use a single file, now i would like to run this script on all the files in a directory!
Please help, i'm not an experienced PHP programmer and i take much joy in the explanation lines in the scrips, gives me and anybody who is whatching this post a learning curve ;)
The script u could use on a single file is something like this:
<?php
$stat = stat('/volume1/Ipcam/_Test/00A8F700CB18()_1_20140107000223_3673.jpg');
$motdate = ($stat['ctime']);
$newname = (gmdate("D d-m-Y_H-i-s", $motdate));
rename("/volume1/Ipcam/_Test/00A8F700CB18()_1_20140107000223_3673.jpg" . "/volume1/Ipcam/_Test/" . $newname . ".jpg");
?>
any help would be appreciated!
Read into scandir or readdir php functions.
They read a bunch of files in a specified directory and returns an array of file names.
You can then loop through these files and apply the above code to each file.
The examples on php.net are pretty easy to use and modify :)
You can use this modification of your code
<?php
$dir = '/volume1/Ipcam/_Test/';
$files = scandir($dir);
foreach($files as $file) {
$stat = stat($file);
$motdate = ($stat['ctime']);
$newname = (gmdate("D d-m-Y_H-i-s", $motdate));
rename($file, $dir . $newname . ".jpg");
}
?>

check if file exists in directory with .css extension based on social engine playlist title

I'm struggling to wrap my head around some code im working on with a social engine based site. the basics of what I want to accomplish is to check if a file exists in a folder called /customcss/ for each playlist title with a .css extension. $playlist->getTitle is used to get the playlist title.
Can someone point me in the right direction? I've been struggling with this for a few hours now, and since SE isnt open source, there is little info on the web.
Here is my code so far, which is not yeilding any error, but isnt working either...
<?php
$filename = '/customcss/$playlist->getTitle.css';
if (file_exists($filename)) {
echo 'official';
} else {
echo 'not official';
}
?>
Best,
$filename = '/customcss/'.$playlist->getTitle.'.css';
OR
$filename = "/customcss/{$playlist->getTitle}.css";
$filename = '/customcss/$playlist->getTitle.css';
would pick from root
please use an absolute path
like
$filename = _DIR_ . '/customcss/$playlist->getTitle.css';
Vicky Biswas

PHP: how do I copy a temp file upload to multiple places?

how can I copy two times the same file? I'm trying to do something like this:
copy($file['tmp_name'], $folder."1.jpg");
copy($file['tmp_name'], $folder."2.jpg");
copy($file['tmp_name'], $folder."3.jpg");
And how many time does temp files has before it's destroyed by the server?
I try using move_uploaded_file also, but I can't make it work. I want to generate 2 thumbs from an uploaded file.
Some help?
Thanks,
move_uploaded_file will move the file, and not copy it -- which means it'll work only once.
If you are using copy, there shouldn't be any limit at all on the number of times you can copy : the temporay file created by the upload will only be destroyed at the end of the execution of your script (unless you move/delete it before, of course)
Still, maybe a solution would be to use move_uploaded_file first, and, then, copy ?
A bit like that, I suppose :
if (move_uploaded_file($file['tmp_name'], $folder . '1.jpg')) {
copy($folder . '1.jpg', $folder . '2.jpg');
copy($folder . '1.jpg', $folder . '3.jpg');
}
This would allow you to get the checks provided by move_uploaded_file...
If this doesn't work, then, make sure that :
$folder contains what you want -- including the final /
That $file['tmp_name'] also contains what you want (I'm guessing this is some kind of copy of $_FILES -- make sure the copy of $_FILES to $file is done properly)
Why doesn't move_uploaded_file() work? Are you trying to use it twice? You can't do that, it moves it, so the second time will fail.
I would just use move_uploaded_file() once, and then make the second copy from the location you just moved it to:
move_uploaded_file($uploaded, $destination);
copy($destination, $destination2);
I don't have a reply to your question directly but how about this workaround ?
copy($file['tmp_name'], $folder."1.jpg");
copy($folder."1.jpg" , $folder."2.jpg");
copy($folder."1.jpg" , $folder."3.jpg");
Thanks man, you give me the light.
I made something like this:
$objUpload = new Upload();
$filename = $objUpload->uploadFile($newFile,$folder);
// returns a string
$objUpload->makeThumb($filename,$folder,"thumbs",139);
// makes a 139px thumbnail from the original file uploaded on the first step
$objUpload->makeThumb($filename,$folder,"mini",75);
// makes another thumb from the same file
Using move_ulploaded_file and copy we can make only one thumb. :)

Categories