I want to open a directory in php and then display the link for downloading that file.
this is what I am trying.
if(is_dir($dir))
{
$op=opendir($dir);
echo"Files in the directiry are<br>";
while(($file=readdir($op))!==false)
{
if(strpos($file,$ext,1))
{
echo "".$file."<br>";
}
}
}
this shows downloading links but only upto space.
PHP function rawurlencode apparently gives you better system coverage. urlencode actually doesn't work on my localhost.
<?php
$dir = 'apps';
$ext = 'pdf';
if(is_dir($dir))
{
$op=opendir($dir);
echo"Files in the directory are<br>";
while(($file=readdir($op))!==false)
{
if(strpos($file,$ext,1))
{
echo '' . $file . '<br>';
}
}
}
?>
More on the subject here: urlencode vs rawurlencode?
Related
I want to make a direct link of a .jpg image using codeigniter.
Here is my code sample.
function download_menu() {
$this->load->helper('download');
$fileName = "foodmenu.jpg";
$file = realpath("images") . "\\" . $fileName;
if (file_exists($file)) {
$data = file_get_contents($file);
force_download($fileName, $data);
} else {
redirect(base_url());
}
}
The following code works fine in WAMP Server but in real server it redirects the link to base_url. according to code it means in real server does not finds the file, hence images folder and foodmenu.jpg exists.
Any help ????
It seems not getting the path on server
$file = file_get_contents(base_url()."images/folder/if/any/$fileName");
if (file_exists($file)) {
force_download($fileName, $file);
} else {
redirect(base_url());
}
I'm trying to copy multiple files from one domain on a web server to another using copy() and looping through a list of files, but it's only copying the last file on the list.
Here is the contents of files-list.txt:
/templates/template.php
/admin/admin.css
/admin/codeSnippets.php
/admin/editPage.php
/admin/index.php
/admin/functions.php
/admin/style.php
/admin/editPost.php
/admin/createPage.php
/admin/createPost.php
/admin/configuration.php
This script runs on the website that I'm trying to copy the files to. Here's the script:
$filesList = file_get_contents("http://copyfromhere.com/copythesefiles/files-list.txt");
$filesArray = explode("\n", $filesList);
foreach($filesArray as $file) {
$filename = trim('http://copyfromhere.com/copythesefiles' . $file);
$dest = "destFolder" . $file;
if(!#copy($filename, $dest))
{
$errors= error_get_last();
echo "COPY ERROR: ".$errors['type'];
echo "<br />\n".$errors['message'];
} else {
echo "$filename copied to $dest from remote!<br/>";
}
}
I get the affirmative message for each and every file individually just as I should, but when I check the directory, only the last file from files-list.txt is there. I've tried changing the order, so I know the problem lies with the script, not any individual file.
The output from the echo statements looks something like this:
http://copyfromhere.com/copythesefiles/admin/admin.css copied to updates/admin/editPage.php from remote!
http://copyfromhere.com/copythesefiles/admin/admin.css copied to updates/admin/editPost.php from remote!
http://copyfromhere.com/copythesefiles/admin/admin.css copied to updates/admin/index.php from remote!
Etc
I've modified your code slightly, and tested it on my local dev server. The following seems to work:
$fileURL = 'http://copyfromhere.com/copythesefiles';
$filesArray = file("$fileURL/files-list.txt", FILE_IGNORE_NEW_LINES);
foreach ($filesArray as $file) {
$fileName = "$fileURL/$file";
$dest = str_replace($fileURL, 'destFolder', $fileName);
if (!copy($fileName, $dest)) {
$errors= error_get_last();
echo "COPY ERROR: ".$errors['type'];
echo "<br />\n".$errors['message'];
}
else {
echo "$fileName copied to $dest from remote!<br/>";
}
}
This uses the same fix that Mark B pointed out, but also consolidated the code a little.
Unless the data you're fetching from that remote site has leading/ in the path/filename, you're not generating proper paths:
$file = 'foo.txt'; // example only
$dest = "destFolder" . $file;
produces destFolderfoo.txt, and you end up littering your script's working directory with a bunch of wonky filenames. Perhaps you wanted
$dest = 'destFolder/' . $file;
^----note this
instead.
I'm having difficulty in copying an image from one folder to another, now i have seen many articles and questions regarding this, none of them makes sense or work, i have also used copy function but its giving me an error. " failed to open stream: No such file or directory" i think the copy function is only for files. The image i wanna copy is present in the root directory. Can anybody help me please. What i am doing wrong here or is there any other way???
<?php
$pic="somepic.jpg";
copy($pic,'test/Uploads');
?>
You should write your code same as below :
<?php
$imagePath = "/var/www/projectName/Images/somepic.jpg";
$newPath = "/test/Uploads/";
$ext = '.jpg';
$newName = $newPath."a".$ext;
$copied = copy($imagePath , $newName);
if ((!$copied))
{
echo "Error : Not Copied";
}
else
{
echo "Copied Successful";
}
?>
You should have file name in destination like:
copy($pic,'test/Uploads/'.$pic);
For your code, it must be like this:
$pic="somepic.jpg";
copy($pic,'test/Uploads/'.$pic);
Or use function, like this:
$pic="somepic.jpg";
copy_files($pic,'test/Uploads');
function copy_files($file_path, $dest_path){
if (strpos($file_path, '/') !== false) {
$pathinfo = pathinfo($file_path);
$dest_path = str_replace($pathinfo['dirname'], $dest_path, $file_path);
}else{
$dest_path = $dest_path.'/'.$file_path;
}
return copy($pic, $dest_path);
}
I am facing a problem. I have some images stored in D://Images. I have to copy these images into D://Images/Modified. I tried but I did not get any output.
This is my code:
define("BASE_IMAGE_PATH","D:\\");
define("IMAGE_FOLDER_NAME","2014finalfour\\");
define("IMAGE_FOLDER_NAME_MODIFIED","modified");
define("IMAGE_File_Path",BASE_IMAGE_PATH.IMAGE_FOLDER_NAME);
define("IMAGE_File_Path_Modified", BASE_IMAGE_PATH.IMAGE_FOLDER_NAME.IMAGE_FOLDER_NAME_MODIFIED);
$srcdir=constant("IMAGE_File_Path");
$destdir=constant("IMAGE_File_Path_Modified");
echo $destdir;
if (!file_exists(IMAGE_File_Path_Modified))
{
mkdir(IMAGE_File_Path_Modified, 0777, true);
}
$srcdir=opendir($srcdir);
while($readFile = readdir($srcdir))
{
if($readFile != '.' && $readFile != '..')
{
if (!file_exists($readFile))
{
if(copy($srcdir . $readFile, $destdir . $readFile))
{
echo "Copy file";
}
else
{
echo "Canot Copy file";
}
}
}
}
closedir($srcdir);
Please help me to sought out it. It says it can not copy the file. copy() expects parameter 1 to be a valid path, resource given in C:\wamp\www\marcs\testmysql.php on line 3
You could use the copy() function :
copy('foo/test.php', 'bar/test.php');
example:
<?php
$file = 'images/folder/one.jpg';
$newfile = 'Images/folder/one_thumb.jpg';
if (!copy($file, $newfile)) {
echo "failed to copy";
}
Makes a copy of the file source to dest.
If the destination file already exists, it will be overwritten.
My Web application stored in directory of XAMPP/htdocs/projectname/. And I have images(source) & img(destination) folders in above directory.I am writing following line of code to get the copy images from one folder to another. But I get the following Warnnigs: (Warning: copy(Resource id #3/image1.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs) and images are not copied into destination.
<?php
$src = opendir('../images/');
$dest = opendir('../img/');
while($readFile = readdir($src)){
if($readFile != '.' && $readFile != '..'){
if(!file_exists($readFile)){
if(copy($src.$readFile, $dest.$readFile)){
echo "Copy file";
}else{
echo "Canot Copy file";
}
}
}
}
?>
Just a guess (sorry) but I don't believe you can use $src = opendir(...) and $src.$readFile like that. Try doing this:
$srcPath = '../images/';
$destPath = '../img/';
$srcDir = opendir($srcPath);
while($readFile = readdir($srcDir))
{
if($readFile != '.' && $readFile != '..')
{
/* this check doesn't really make sense to me,
you might want !file_exists($destPath . $readFile) */
if (!file_exists($readFile))
{
if(copy($srcPath . $readFile, $destPath . $readFile))
{
echo "Copy file";
}
else
{
echo "Canot Copy file";
}
}
}
}
closedir($srcDir); // good idea to always close your handles
Replace this line in your code, this will work definitely.
if(copy("../images/".$readFile, "../img/".$readFile))
you are giving wrong path ,if path of your file say script.php is "XAMPP/htdocs/projectname/script.php" and images and img both are in "projectname" folder than you should use following values for $srcPath and $destPath,change their values to
$srcPath = 'images/';
$destPath = 'img/';
public function getImage()
{
$Path='image/'; //complete image directory path
$destPath = '/edit_image/';
// makes new folder, if not exists.
if(!file_exists($destPath) || file_exists($destPath))
{
rmdir($destPath);
mkdir($destPath, 0777);
}
$imageName='abc.jpg';
$Path=$Path.$imageName;
$dest=$destPath.$imageName;
if(copy($Path, $dest));
}