So i'm currently trying to create a code which simply creates and publishes a file to my webroot, modifies and writes to that file, and then finally change the location of the file to another directory/folder using move_uploaded_file()
This is my code so far
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,'$dir/$title.txt');
fwrite($myfile, $_POST['textarea11']);
fclose($myfile);
The code doesn't work, i've tried echoing move_uploaded_file() and it returned nothing, however the file was uploaded but it's location just wasn't changed.
$dir is defined as $dir = __DIR__.'/../uploads/'; and $title is define as $title = $_POST['title'];
move_uploaded_file() can only be used if you are submitting a multipart form and you want to save the uploaded file.
What you probably need is this:
http://php.net/manual/en/function.rename.php
Change your given code as
$dir = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'uploads';
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,"$dir".DIRECTORY_SEPARATOR."$title.txt");
fwrite($myfile, $_POST['textarea11']);
In your code
move_uploaded_file($myfile,'$dir/$title.txt');
php variable $dir and $title value is not coming. and value of $dir is consisting '/' and you are adding one more to make full file path too.
Always use directory separator to run in all Operating System. some OS use '/' and some OS use '\'.
Related
my link (URL) is different!!! and does not work with usual method
I think because site load with js or aspx
you can test my link (URL) in your browser and see download starting
but cant work in php
I have tested all methods (fetch, curl, file, get, put), but it does not work.
I have a similar URL here: 'http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'
I can open it in the browser and download a csv file I need to do this in php and save the csv file on server
Here is what I have tried so far:
<?php
$file = fopen('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
$file = file_get_contents('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
file_put_contents('ClientTypeAll.csv', $file);
?>
I do not want Contents !!! I want a csv file form my link
if you test my link in your browser download start in your pc
I run this code with a remote PDF file.
<?php
$url = 'https://example.com/file.pdf';
$dir_name = 'storage-x'; // saVe the directory name
if (!file_exists($dir_name)) {
mkdir($dir_name, 0777, true);
}
$path = $dir_name.'/'.rand().'.pdf';
$file = file_get_contents($url);
file_put_contents($path, $file);
?>
Please follow the below step.
Get file form URL.
Set directory and check file exit condition and update directory access permission.
Set new file path, name, and directory.
Save the file.
Please check the below example which works for me.
<?php
$file = file_get_contents('https://example.com/file.pdf');
$dirName = 'storage-pdf';
if (!file_exists($dirName)) {
mkdir($dirName, 0777, true);
}
$newFilePath = $dirName.'/'.rand().'.pdf';
file_put_contents($newFilePath, $file);
?>
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");
This question already has answers here:
Create or write/append in text file
(7 answers)
Closed 4 years ago.
I want to create a file failure-log.log in a specific directory and write to it. Already got the directory path from database. The path is like this:
D:/folder-one/folder-two/
And my PHP code is executing in another directory like this:
C:/apache24/crawler/admin/startService.php
How can I create the file and write to it?
Make sure you use absolute path(you can use realpath() on relative path as well to be sure about the path) and directory is writable
then
$dir = 'your/path';
file_put_contents($dir ."/failure-log.log", $contentOfFile);
if you want the content of file not to be removed everytime then I suggest using FILE_APPEND
file_put_contents($dir ."/failure-log.log", $contentOfFile, FILE_APPEND);
Make that folder location writable for web server first. Then use below code to create file in that location.
$myfile = fopen("D:/folder-one/folder-two/file.log", "a") or die("Unable to open location for log file !");
$txt = "Log details goes here ...";
fwrite($myfile, $txt);
fclose($myfile);
Use file_put_contents method, like this:
$file ="D:/folder-one/folder-two/";
$current = file_get_contents($file);
$current .= 'yourcontenthere';
file_put_contents($file, $current);
U can send flags to file_put_contents, like FILE_APPEND :
$file ="D:/folder-one/folder-two/";
$text = 'yourcontenthere';
file_put_contents($file, $text, FILE_APPEND);
In that case u will not have to retrieve the old content, u can check and other flags in link above.
Also is good idea to check if file exists before that.
Write to files
$writeFile = #fopen('/path/to/save/file', 'w+');
#fwrite($writeFile, $content);
#fclose($writeFile);
With:
w+: will create a new file if it does not exist and overwrite if it exists
a: append to file already exist
a+: append to file already exist and create a new file if it does not exist
If you load path directory from the database, you maybe need to create multi-directory
if( !is_dir($path) ) {
mkdir($path, 0777, true);
}
With:
$path: path you were loaded from db
I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}
I have one image in a directory on my server, and want to copy it to another directory.
So I'm using
$post_picture = 'http://mysite.com/image.jpg';
copy($post_picture,
'images/pictures/post/thumb/' .
$info['filename'] .
'_thumb.' .
$info['extension']);
The issue is that in fact a file is created in my thumb directory, but that image is empty (0 x 0 pixels). I get no errors.
Any idea what is happening?
Permissions on all dirs are 755, both original and copy image have 644. The original show normally on a browser.
Thanks.
Do you have any form of hot-link protection that could alter what php receives?
Is allow_url_fopen allowed?
The $post_picture variable should probably have the file system path to the file, rather than the URL to the file.
Is allow_url_fopen set to true in your php.ini?
Sometimes that can produce this result if remote connections are being blocked.
$post_picture should be a local path i believe
copy( '/path/image.jpg', ... );
Please use this one...
<?php
$source = 'f-1/2.jpg';
$destination = 'f-2/2.jpg';
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
?>