d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Chmod A Ftp File With Curl > How?
Add Reply New Topic New Poll
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Feb 23 2013 10:12pm
My code connects to the ftp client and uploads a specified file but not the contents of it because there aren't default permissions set for guests to write to files.

So because of this I have to find a way to send a chmod change request to the file after it's been uploaded so that my program can write to it.

But that's my problem is that I don't know what command to send to the file to change the permissions.

I've tried a few things such as CHMOD 777 <file> and such but haven't had any luck.

I know that you send requests with this method:
Code
struct curl_slist* headerlist = NULL;
   std::string cmd1 = "";
   headerlist = curl_slist_append(headerlist, cmd1.c_str());
   curl_easy_opt(curl, CURLOPT_POSTREQUEST, headerlist);


But like I said, I have been unsuccessful with sending a chmod request.

I don't know anything about PHP but I was thinking that maybe there could be a way to pass the file path to a PHP script and change the permissions that way?

Here's the upload code:

Code
bool UploadFile(char* URL, const char* path)
{
CURL * curl;
CURLcode res;
curl_off_t fileSize;

FILE * uploadFile;

std::string RENAME_FILE_TO;

struct stat file_info;
struct curl_slist* headerlist = NULL;
std::string cmd1 = "CHMOD 777 ";

SetNewFileName(RENAME_FILE_TO, URL);

cmd1 += RENAME_FILE_TO;

std::cout << cmd1 << std::endl;

if(stat(path, &file_info))
{

}
fileSize = (curl_off_t)file_info.st_size;

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();

if(curl)
{
  uploadFile = fopen(path, "rb");
 if(uploadFile != NULL)
 {
  headerlist = curl_slist_append(headerlist, cmd1.c_str());

  curl_easy_setopt(curl, CURLOPT_READFUNCTION, readData);
  curl_easy_setopt(curl, CURLOPT_UPLOAD, true);
  curl_easy_setopt(curl, CURLOPT_URL, RENAME_FILE_TO.c_str());
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  curl_easy_setopt(curl, CURLOPT_READDATA, uploadFile);
  curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
  curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
 

  res = curl_easy_perform(curl);

  if(res != CURLE_OK)
  {
   printf("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  }

  fclose(uploadFile);
 
 }
 else
 {
  std::cout << "Failed to open " << path << "!\n";
  return false;
 }

 curl_slist_free_all(headerlist);
 curl_easy_cleanup(curl);
}
else
{
 std::cout << "Failed to initialize cURL!\n";
 return false;
}

curl_global_cleanup();

return true;
}


This post was edited by SelfTaught on Feb 23 2013 10:28pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 23 2013 11:21pm
im not sure about the library itself but from the command line it looks something like this

Code
 curl -T infile ftp://upload.com/coolfile -Q '-SITE CHMOD 777 my_directory'
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Feb 25 2013 12:44pm
Quote (AbDuCt @ Feb 23 2013 09:21pm)
im not sure about the library itself but from the command line it looks something like this

Code
curl -T infile ftp://upload.com/coolfile -Q '-SITE CHMOD 777 my_directory'


Thanks, I got it working.

The command I was trying (CHMOD <permissions> <file>) was correct. I was using the wrong request, CURLOPT_HTTPHEADER, when I should have been using CURLOPT_POST (request for file transfer protocols) instead.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll