I need to create a folder using mkdir(); in PHP.
But the issue that I have is that the $path is a full URL as opposed to full path on the server.
So i did a search on Google and some say it is possible and some say its not and I found this small code spinets on STO which is here:
Php mkdir() is not creating a directory in my web directory
$path = "https://wwww.domain.com/astuces/uploads/products/NICE;
if(!is_dir($path)){
$old_umask = umask(0);
mkdir($path, true);
chmod($path, 0777);
umask($old_umask);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}
However, the code above doesn't really do anything.. it doesn't create any folder called NICE in the given $path string.
My question is, is this even possible USING PHP and if so how.. and what are my other options to achieve this if this is not possible using PHP?
Thanks in advance.
Related
My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely
EDIT: I'm pretty sure the issue has to do with the firewall, which I can't access. Marking Canis' answer as correct and I will figure something else out, possibly wget or just manually scraping the files and hoping no major updates are needed.
EDIT: Here's the latest version of the builder and here's the output. The build directory has the proper structure and most of the files, but only their name and extension - no data inside them.
I am coding a php script that searches the local directory for files, then scrapes my localhost (xampp) for the same files to copy into a build folder (the goal is to build php on the localhost and then put it on a server as html).
Unfortunately I am getting the error: Warning: copy(https:\\localhost\intranet\builder.php): failed to open stream: No such file or directory in C:\xampp\htdocs\intranet\builder.php on line 73.
That's one example - every file in the local directory is spitting the same error back. The source addresses are correct (I can get to the file on localhost from the address in the error log) and the local directory is properly constructed - just moving the files into it doesn't work. The full code is here, the most relevant section is:
// output build files
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
copy($source, $dest);
echo "Copy $source to $dest. <br>";
}
}
You are trying to use URLs to travers local filesystem directories. URLs are only for webserver to understand web requests.
You will have more luck if you change this:
copy(https:\\localhost\intranet\builder.php)
to this:
copy(C:\xampp\htdocs\intranet\builder.php)
EDIT
Based on your additional info in the comments I understand that you need to generate static HTML-files for hosting on a static only webserver. This is not an issue of copying files really. It's accessing the HMTL that the script generates when run through a webserver.
You can do this in a few different ways actually. I'm not sure exactly how the generator script works, but it seems like that script is trying to copy the supposed output from loads of PHP-files.
To get the generated content from a PHP-file you can either use the command line php command to execute the script like so c:\some\path>php some_php_file.php > my_html_file.html, or use the power of the webserver to do it for you:
<?php
$hosted = "https://localhost/intranet/"; <--- UPDATED
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$path = str_replace("\\","/",$path); <--- ADDED
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
$content = file_get_contents(urlencode($source));
file_put_contents(str_replace(".php", ".html", $dest), $content);
echo "Copy $source to $dest. <br>";
}
}
In the code above I use file_get_contents() to read the html from the URL you are using https://..., which in this case, unlike with copy(), will call up the webserver, triggering the PHP engine to produce the output.
Then I write the pure HTML to a file in the $dest folder, replacing the .php with .htmlin the filename.
EDIT
Added and revised the code a bit above.
I'm creating a PHP script, which supposed to extract a zip archive stored on the php file directory to a folder.
Everything works well, but when I check te result, I find 2 folders under the directory: a folder with the name of the zip archive, and another folder named __MACOSX. I don't know how this folder came there, especially as I'm using Windows 7. Second, in each folder there is a file called .DS_Store.
Now, I don't know how these things got there. This is my code:
$zip = new ZipArchive;
if ($zip->open('File.zip')) {
$path = getcwd() . "/details/" . trim($id) . "/";
$path = str_replace("\\","/",$path);
echo $path;
echo $zip->extractTo($path);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This is the only code that extracts the zip file, or touching it, and as you can see, there is nothing like __MACOSX or .DS_Store.
Can you please help me?
File.zip originated on a OSX system. __MACOSX and .DS_Store have 0 usage or bearing on any other OS. Delete / Ignore them and keep trucking.
As an aside, you may want to add the stated file system objects to your project .gitignore.
https://superuser.com/questions/104500/what-is-macosx-folder
https://en.wikipedia.org/wiki/.DS_Store
I am trying to create folder and sub folder on website. Code is pretty simple. Not sure why does not work.
<?php
$domain = "officeactionuspto.com";
mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain), 0777, true);
mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images'), 0777, true);
$folder= $_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images';
if(is_dir($folder))
{
echo ("$folder is a directory");
}
else
{
echo ("$folder is not a directory");
}
?>
You don't have to use absolute path to create a directory.
You can just do it with following code:
mkdir('images', 0777, true);
$folder= 'images';
if(is_dir($folder)){
echo ("$folder is a directory");
}else{
echo ("$folder is not a directory");
}
you can also get the absolute path after created, if you desired:
$full_path = realpath('images');
PS: I'm supposing you are executing this code on /index.php, if was on another different structure, you need to write the relative path for it.
EDIT: I tested and eliminate a parentheses on mkdir and works.
You don't need to use the entire, absolute filename. You just need to use the path relative to the folder where the script being executed is located.
Although I don't know your file structure, lets pretend that the PHP script is in the crc folder: Your command would be: mkdir(('/website_templates/client_files/'.$domain), 0777, true);
EDIT: With the recursive parameter, you can create the images subfolder in the same command.
I'm using codeigniter to create a CSV file, and I can write successfully inside the application structure, but not outside. The reason why I want to create the file outside the application structure cause I get a 403 permissions error when linking to the file.
Either my folder permissions are wrong (I've used 777) or my code is wrong. Please help.
The application is sitting at: domain.com/mysite/ci/
The files created in: domain.com/mysite/ci/_/files/ (I can create the file here, but can't link to download it
I would like to create the file in: domain.com/mysite/downloads/ (I cannot create the file here, but I can link stuff to it to download.
CodeIgniter
$this->load->dbutil();
$this->load->helper('file');
$delimiter = ",";
$newline = "\r\n";
$query = $this->db->query("SELECT * FROM songlist");
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);
$filePath = '_/files/songlist.csv';
echo "filePath=". $filePath. "</br>";
if (! write_file($filePath, $data)){
echo 'not done';
} else {
echo anchor(base_url(). $filePath);
}
}
I think the problem is that "_" is not a valid folder for web. Try changing it.
If you want to create the file in "mysite/downloads" you filepath would be:
$filePath = '../downloads/songlist.csv';
And i see no reason you shouldn't be able to create it there.