How is one supposed to handle files that aren't in the current directory when using ftp_put? This piece of code is trying to upload a file that I know exists, but it always gives the following error:
"Warning: ftp_put() [function.ftp-put]: Requested action not taken, file not found or no access. in /path/to/files/domains/mydomain.com/html/scriptfile.php on line 1337"
Here's the snip:
$file_name = $this->GetFileName();
if ($file_name)
{
$resource = ftp_connect('ftp.remoteftpserver.com');
if ($resource && ftp_login($resource, $username, $pass))
{
ftp_pasv($resource, true);
//UPLOAD_DIRECTORY == '/IN' (it really exists, I'm sure)
//ORDER_DIRECTORY == /home/domains/mydomain.com/orders (came from $_SERVER['DOCUMENT_ROOT']
ftp_put($resource, UPLOAD_DIRECTORY . '/' . $file_name, ORDER_DIRECTORY . '/' . $file_name, FTP_ASCII);
ftp_close($resource);
}
else
{
echo "FTP Connection Failed!";
}
}
Check the permissions of the remote file. Make sure $username has write access to the file. Make sure you have execute access on the parent directory.
Related
What I'm trying to do:
Use PHP ftp_nlist to retrieve the contents of a directory on the FTP server
The problem:
For directories that contain a lot of files (the one I encountered the problem on has nearly 40 thousand files, and no subfolders), the ftp_nlist function is returning false. For directories that are not as large, the ftp_nlist function returns an array of filenames as expected.
What I've tried:
Enabling passive mode (it already was enabled, but I see it as a common suggestion)
Adding ftp_set_option($conn_id, FTP_USEPASVADDRESS, false); after my ftp_login
using ftp_chdir, although my folder names never have spaces anyways
echoing error_get_last() after ftp_nlist returns false. The error show seems unrelated, but is shown below.
My code:
In case it is useful, here is the function I have created. What it is supposed to do is...
take in $fm (filemaker, unrelated to this problem)
take in $FTPConnectionID (the ftp connection I established in the prior to the function call)
take in $FolderPath (the path of the folder on the FTP server for which I want to list files/subfolders recursively - ex: "SomeFolder/Testing")
take in $TextFile (I am writing the paths of every file found on the FTP server to a text file, which was created prior to calling the function)
function createAuditFile($fm, $FTPConnectionID, $FolderPath, $TextFile) {
echo "createAuditFile called for " . $FolderPath . "\n";
//Get the contents of the given path. Will include files and folders.
$FolderContents = ftp_nlist($FTPConnectionID, $FolderPath);
if($FolderContents == false) {
echo "Couldn't get " . $FolderPath . "\n";
echo "ERROR: " . print_r(error_get_last()) . "\n";
} else {
print_r($FolderContents);
}
//Loop through the array, call this function recursively if a folder is found.
if(is_array($FolderContents)) {
foreach($FolderContents as $Content) {
//Create a varaible for the folder path
$ContentPath = $FolderPath . "/" . $Content;
//Call the function recursively if a folder is found
if(pathinfo($Content, PATHINFO_EXTENSION) == "") {
createAuditFile($fm, $FTPConnectionID, $ContentPath, $TextFile);
echo "Recursive call for " . $ContentPath . "\n";
//If a file is found, add the file ftp path to our array
} else {
echo "Writing to file: " . $ContentPath . "\n";
fwrite($TextFile, $ContentPath . "\n");
}
}
}
}
I can provide other code if needed, but I think my question is less of a coding issue, and more of an understanding ftp_nlist issue. I've been stuck on this for hours, so any help is appreciated. And like I said, this function works just fine for most folder paths passed to it, the problem is when there are tens of thousands of files within the folder. Thank you!
When a user creates a profile and uploads a profile picture, the PHP "move_uploaded_file" function is used to move a temporary copy of this image to the project directory.
While my current code works perfectly fine on my Windows 10 machine, it doesn't work on my Mac because of incorrect file permissions.
I have already tried the method provided in other answers and nothing changes after running the terminal command:
sudo chmod 644 /opt/lampp/htdocs/yourfolder
This is true for any variation including:
sudo chmod -R 777 /opt/lampp/htdocs/yourfolder
Note that I DO have admin permissions on the laptop
Other users have stated that these commands have fixed their problems, so I suspect that mine is different.
PHP Code Snippet:
$username = $_POST["username"];
$password = $_POST["password"];
$ext = null;
/* Debugging information
echo $_FILES["profile_pic"]["name"]."<br>";
echo $_FILES["profile_pic"]["tmp_name"]."<br>";
echo $_FILES["profile_pic"]["size"]."<br>";
echo $_FILES["profile_pic"]["error"]."<br>";
*/
if (strrpos($_FILES["profile_pic"]["name"], ".jpg") != null || strrpos($_FILES["profile_pic"]["name"], ".jpeg") != null) {
$ext = ".jpg";
};
if (strrpos($_FILES["profile_pic"]["name"], ".png") != null) {
$ext = ".png";
};
$path = __DIR__ . "\data\users\images\\" . uniqid() . $ext;
if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $path)) {
echo("File successfully uploaded");
} else {
echo("Upload failed");
};
Returned: Upload failed
Error thrown:
move_uploaded_file(/opt/lampp/htdocs/Test-Site\data\users\images\5c1dadd52bb71.jpg): failed to open stream: Permission denied in /opt/lampp/htdocs/Test-Site/signup_confirm.php on line 27
Expected: user profile picture is inserted into the .../htdocs/Test-Site/data/users/images folder
Actual: above error about incorrect file permissions is thrown
The reason for the error is that I was using incorrect directory separators ("\" instead of "/" for Mac). Changing these made the profile picture move to the intended location.
I have been using an upload script on my server, like below
$newname = time() . '_' . $_FILES[$file]["name"];
if (strtolower(end(explode('.', $_FILES[$file]["name"]))) != 'pdf' AND $file != "damage_attachment_damageform_1" AND $file != "damage_attachment_damageform_2" AND $file != "damage_attachment_damageform_3" AND $file != "damage_attachment_damageform_4") {
if (move_uploaded_file($_FILES[$file]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/components/com_fleet/uploads/docs/' . $newname)) {
$images[] = $_SERVER['DOCUMENT_ROOT'] . '/components/com_fleet/uploads/docs/' . $newname;
$docs[] = $_SERVER['DOCUMENT_ROOT'] . '/components/com_fleet/uploads/docs/' . $newname;
} else {
die();
}
}
It uploads an image fine, but since a few days a get a Warning: move_uploaded_file(): Unable to move error. Ive seen these a dozen of times while learning to program, so I did all the usual stuff, check paths, the $_FILES[$file]["error"] and check all the right CHMODs. All is fine, path is spot-on, chmod is too, no errors etc...
1 extra weird thing I noticed the file does get written to the right /docs map but its Filesize is empty, and move_upload_file still sends false...
What am I forgetting? CHOWN maybe? And how can I solve that, I dont have SSH access or something.
Graa after an hour I now found out what was wrong, server Disk Quota was exceeded. Maybe people can still benefit from my problems...
I have the following code below, I cannot get why it's not working.
$server=("ftp.blah.com");
$connect=ftp_connect($server);
$dest='/';
$login_result=ftp_login($connect,"blah#blah.com","lol");
if(!($login_result)||!($connect))
{
$error;
} else {
echo "success";
}
$file= 'Tiny-' . $time. '.txt';
$upload=ftp_put($connect,$dest,$file,FTP_ASCII);
if (!$upload)
{
echo "failed to upload";
} else{
echo "successfully uploaded";
}
ftp_close($connect);
When i run the code, i get the error "Warning: ftp_put(Tiny-201201070758.txt): failed to open stream: No such file or directory
I have made the destination folder of the ftp write and read accessible.
I have also tried to include the full path of the text file by:
$file= 'C:\xampp\htdocs\Tiny-' . $time. '.txt'
or
$file= 'C:\\xampp\\htdocs\\Tiny-' . $time. '.txt'
I also tried using FTP_Binary instead of ASCII, still no luck.
nothing works.
Have you got url_fopen active on your server? Check with phpinfo().
I am making a intranet customer manager in PHP. For each customer a directory is created for the shop to add files into. What my script is supposed do is if no directory exists create it, if it does exists dont create it.
What is actually happening is if the directory already exists I am getting the following error :
Warning: mkdir() [function.mkdir]: File exists in C:\server2go\server2go\htdocs\customermgr\administrator\components\com_chronoforms\form_actions\custo m_code\custom_code.php(18) : eval()'d code on line 14
So what is happening it is trying to create it anyway, even though the if statement should stop it ?, im confused on what I am doing wrong :-S .
<?php
$customerID = $_GET['cfid'];
$directory = "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
$thisdir = getcwd();
mkdir($thisdir ."/customer-files/$customerID" , 0777); }
?>
Replace:
if(file_exists($directory) && is_dir($directory)) {
with:
$thisdir = getcwd();
if(file_exists($thisdir.$directory) && is_dir($thisdir.$directory)) {
or better:
<?php
$customerID = $_GET['cfid'];
$directory = "./customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
mkdir($directory , 0777); }
?>
Just took a short look but i would try this:
$directory = $thisdir . "/customer-files/$customerID";
and remove $thisdir from mkdir();
also you should move your $thisdir before the $directory declaration
The function file_exists() does not use relative paths, where is_dir() can. So instead, use the common denominator and pass an absolute path to these functions. Additionally you can move the call to getcwd() into the $directory assignment and reuse $directory later for creating the directory.
<?php
$customerID = $_GET['cfid'];
// Get full path to directory
$directory = getcwd() . "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
// Do nothing
}
else {
// Directory doesn't exist, make it
mkdir($directory , 0777); }
}
?>