I am trying to move a file from the command directory it currently sits in to a new directory called product_upload however the code below does not work properly
//The following creates and saves the file in the directory - this bit works fine. It saves the file in the command directory
$file = fopen($filenamecsv, "w+");
fwrite($file,$contents);
fclose($file);
////The following does not work
$filenamecsv_move = "command/Product_category_list".date("j-m-Y_H.i"). ".csv";
$filenamecsv2_move = "command/product_upload/Product_category_list".date("j-m-Y_H.i"). ".csv";
rename($filenamecsv_move ,$filenamecsv2_move );
Probably directory command/product_upload doesn't exists and rename function won't create it. In this case you have to manually create this directory or add mkdir in your script.
Look at below script. If you have only command directory and commend mkdir line rename function won't rename file and return false (var_dump added at the end).
<?php
$filenamecsv_move = "command/Product_category_list".date("j-m-Y_H.i"). ".csv";
file_put_contents($filenamecsv_move,'testdata');
$filenamecsv2_move = "command/product_upload/Product_category_list".date("j-m-Y_H.i"). ".csv";
if (!file_exists('command/product_upload')) {
mkdir('command/product_upload', true);
}
$result = rename($filenamecsv_move ,$filenamecsv2_move );
var_dump($result);
You should also turn on error_reporting to display errors when you are testing your script. I had the following warning:
Warning:
rename(command/Product_category_list29-05-2014_13.40.csv,command/product_upload/Product_category_list29-05-2014_13.40.csv):
in D:\DaneAplikacji\WampServer\www\rename\index.php on line 12
Related
I have question about how to save log in correctly directory. My php code:
public static function log(test $testination) {
echo($testination->getDetails()."\n");
$log_filename = shell_exec("cd; ~/log/");
$log_filename2 = "log";
if (!file_exists($log_filename2))
{
mkdir($log_filename, 0777, true);
}
$log_file_data =$log_filename.'/logs_' . date('d-M-Y') . '.log';
file_put_contents($log_file_data, var_dump($testination) . "\n", FILE_APPEND);
}
in this code, I create a folder in the same place as my PHP file, then create folder log if this not exist.
I want to save in begining (just cd) then create log folder if this not exist I try this code
$log_filename = shell_exec("cd; ~/log/");
i get warning/error
PHP Warning: file_put_contents(/logs_18-Aug-2020.log): failed to open stream: Permission denied in /test/test/Helper.php on line 318
Any idea how to do it? Please im stuck.
Any help will be appreciated.
That's not how you should use file_put_contents: it does not know anything about that attempt to change the directory, as this will not affect the execution of PHP. If you want to get the directory of the current PHP file, you can use basename(__DIR__)
Additionall, cd; ~/log/ won't work in your shell as well.
I'm currently writting a login-system with PHP, for that I need to read the files with some user-information in it.
But after changing the folder system, PHP fopen doesn't read the files anymore.
Both the users.php and userinf.csv files are in the samle folder.
I allready tried to change the filepath, hard-coded the filepath , recreated the file. All of which file.
//Read file
$fp = fopen("userinf.csv", "r");
if(!$fp)
{
echo "File couldn't be read";
return false;
}
Before changing the file system, it worked. But now I am geting the error:
Warning: fopen(userinf.csv): failed to open stream: No such file or directory in FILEPATH on line 45
When you use the fread function without any reference it could fail. I always say that you need to check your path first with getcwd()
<?php
echo getcwd(); //Current Working Directory
?>
Use absolute paths, always. It removes any ambiguity. Using a relative path may change based on where your script is located, among other things, depending on your system.
$fp = fopen("/home/somewhere/blah/userinf.csv", "r");
You can always use a variable for the path as well:
// Somewhere in your code
define('ROOT_PATH', "/home/somewhere/blah");
// In the implementation
$fp = fopen(ROOT_PATH . "/userinf.csv", "r");
I want to create a new file along with the the creation of a record in my db. The db part works fine, but the file part isn't working. I want to create a file in another folder in the root directory and I used the same code with changes in the file name. Despite setting ini_set('display_errors',3) the code doesn't return any errors, I've also tried fopen($file_name,"w") or die("unable to open file"), still the code ran. I've given IIS_IUSRS full control over the directory. Here is my code
$data = mysqli_fetch_array(mysqli_query($conn,"Select * from content where link='$url'")); //fetch data from db
$id = $data['id'];
$file_name="/files/$id.html"; //sets file name
$file = fopen($file_name,"w"); //creates the file
fwrite($file,$_POST['desc']); //writes to the file
fclose($file); //closes the file
The problem was PHP wasn't going to the root directory. I added $root = $_SERVER['DOCUMENT_ROOT'] and replaced $file_name="/files/$id.html" with $file_name="$root/files/$id.html"; and it worked like a charm.
I am trying to move images from one server to the other, using ftp_put like this
$des = "example.com/images/";
if (ftp_put($new_ftp, $des, $src, FTP_BINARY))
But I am getting this error..
Warning: ftp_put() [function.ftp-put]: Can't open that file: : No such file or directory!
However when setting the destination on the root it does works.
$des = "example.com"; //works
$des = "example.com/images"; //Does NOT work
Any Idea why?
if it is a directory, check whether it exists and put a / after the name:
$des = "example.com/images/";
if the dir wouldn't exist, the sent file would have been named "images"
if it has a slash at the end, it tries to open it as a directory.
The message "No such file or directory!" is the hint: images/ doesn't exist and ftp will not create that for you automatically:
ftp_mkdir ( $new_ftp, $des ); // ignore errors?
Got it!
it should be
$des = "/images/";
since the root is specified at the connection.
I am trying to create a file using php to a dirctory which has cmod 0777 so it should be fine.
Here is the code:
$fh = fopen("/_myfiles/myfile.txt", "w+");
if ($fh==false)
{
die("unable to create file");
}
But all I get is "unable to create file". Any ideas on what it could be?
Note: For the path I've also tried:
$fh = fopen($_SERVER['DOCUMENT_ROOT']."/_myfiles/myfile.txt", "w+");
with no success.
fopen() generates an E_WARNING message on failure.
I recommend using error_reporting(E_ALL) to show the warning and this should help you to troubleshoot the problem from there.
Check write permissions on the directory you want to create the file in.
Also the directory "_myfiles" should exist (it won't be created automatically).
If they are correct, then this will create the file in the same directory where the PHP script is located:
$basedir = dirname(__FILE__);
$fh = fopen($basedir . DIRECTORY_SEPARATOR . 'myfile.txt', 'w+');