Write to tmp folder
Asked Answered
C

4

9

EDIT 1

This question involves the user of the MANGOPAY API. My problem is that I cannot get the API to write to the tmp folder.

I have succeeded in getting the API to write to http://fakedomain.com/apifolder/blank.txt and getting the appropriate output.

I then ran the script http://fake.com/apifolder/demos/iswrite.php on http://fakedomain.com/apifolder/blank.txt to have minimal working code I could test on the tmp folder. Here is the code in the script:

<?php

$filename = 'blank.txt';


echo "<br>";


file_exists($filename);

echo "<br>";



if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

echo "<br>";

if (is_writable($filename)) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}

?>

It gives me the output

The file blank.txt exists The file is writable

so all good there. I created the following file with the following permissions using Filezilla:

enter image description here

In the script http://fake.com/apifolder/demos/iswrite.php I have changed the filename variable to $filename = 'http://fake.com/tmp/creative/blank.txt';. It gives me the following output:

The file http://fake.com/tmp/creative/blank.txt does not exist The file is not writable

Also, allow_url_fopen is on.

I don't fully understand URL structures, so maybe the problem lies there? The tmp folder I am trying to access is on the same level as my public_html folder. Maybe I am writing my URLs wrong?

enter image description here

Put in another way, does the tmp folder have to be outside the public_html folder? Would there be any purpose to this? Or can I have create my own tmp folder within public_html where it is already working?


ORIGINAL QUESTION

The original question was poorly written. Please see EDIT 1

I am playing with the sandbox of an API (MangoPay). I have included my ClientId and ClientPassword which seems to work.

The API also says...

You also need to set a folder path in $api->Config->TemporaryFolder that SDK needs to store temporary files. This path should be outside your www folder. It could be /tmp/ or /var/tmp/ or any other location that PHP can write to.

I have created one at:

ftp://fakedomain@fakedomain/tmp/fakefoldername

I am running the php script from my desktop using Terminal. The output it is giving me is

Cannot create or write to file ftp://fakedomain@fakedomain/tmp/fakefoldername

even though I have set permissions to 777.

Any idea of why I am getting this error?

Cowie answered 6/11, 2015 at 13:28 Comment(8)
Which OS are you using?Kunkel
@Fred-ii- I'm using Linux with CPanelCowie
Thanks. I added the relevant tag and removed the "http", if that's ok. This could help you get better exposure and related to the OS in question.Kunkel
@Fred-ii- I'll take all the help I can get!Cowie
I'm not sure if this will be of help, but see if error reporting (if your system's not already set to catch/display errors/warnings/notices) php.net/manual/en/function.error-reporting.php - I'll see what I can find in the meantime Eric.Kunkel
Is there a reason you are using ftp for the temporary file as part of your testing? Have you tested connecting to the ftp service and creating a file manually?Hydrostatic
Eric, tell us what exactly you put in $api->Config->TemporaryFolder = '/some/path/';Bjork
@RandomSeed You are 100% correct. Apologies for that. I have been out of the game for a while, so a bit rusty. The question has now been edited. I hope that brings clarity.Cowie
G
1

Generally speaking a temp dir is supposed to be located on the same machine as your code. Using FTP is less than ideal. Are you not able to use a local directory?

You'll notice in the MangoPay documentation it shows a local dir:

https://github.com/Mangopay/mangopay2-php-sdk

$api->Config->TemporaryFolder = '/some/path/';

You should probably stick to that, instead of using a remote server via FTP.

Getaway answered 13/11, 2015 at 2:31 Comment(3)
Despite my poor wording, I believe you understood what I was doing wrong here. Would it be possible to check my edit and confirm my suspicions? (see bottom of edit in bold). Thanks!Cowie
To answer your other questions: this tmp folder is going to house files that are not supposed to be visible from the web (eg, not visible by users in their web browser) since it will contain stuff users are not supposed to be able to access. Putting it along-side the public_html folder is fine, but don't expect to be able to access it like this http://fake.com/tmp/, since that's not what the tmp folder is for.Getaway
Also, according to their documentation, if you have a sandbox, the tmp dir must be different than your non-sandbox site, so make two different mp dirs if needed.Getaway
C
5

I am guessing the library in question is this one, and the error you are getting is this exception:

 if (!is_writable($this->GetPathToTemporaryFolder()))
            throw new \MangoPay\Libraries\Exception('Cannot create or write to file ' . $this->GetPathToTemporaryFolder());

So basically we seem to be debugging a call to is_writable().

  1. make sure allow_url_fopen is on
  2. if applicable, make sure the URL includes the FTP password
  3. make sure the folder exists and the FTP account has write permissions (777 should suffice...)
Continuo answered 8/11, 2015 at 23:35 Comment(0)
G
1

Generally speaking a temp dir is supposed to be located on the same machine as your code. Using FTP is less than ideal. Are you not able to use a local directory?

You'll notice in the MangoPay documentation it shows a local dir:

https://github.com/Mangopay/mangopay2-php-sdk

$api->Config->TemporaryFolder = '/some/path/';

You should probably stick to that, instead of using a remote server via FTP.

Getaway answered 13/11, 2015 at 2:31 Comment(3)
Despite my poor wording, I believe you understood what I was doing wrong here. Would it be possible to check my edit and confirm my suspicions? (see bottom of edit in bold). Thanks!Cowie
To answer your other questions: this tmp folder is going to house files that are not supposed to be visible from the web (eg, not visible by users in their web browser) since it will contain stuff users are not supposed to be able to access. Putting it along-side the public_html folder is fine, but don't expect to be able to access it like this http://fake.com/tmp/, since that's not what the tmp folder is for.Getaway
Also, according to their documentation, if you have a sandbox, the tmp dir must be different than your non-sandbox site, so make two different mp dirs if needed.Getaway
S
0

An out-of-the-box Linux machine running Ubuntu 14.04 has the following user and permission settings

drwxrwxrwt 13 root root 4096 nov  9 00:56 tmp//

And the dummy directory

drwxrwxr-x 2 eric eric 4,0K nov  9 00:49 fakefoldername/

In case of switching to 777 permission set (as you said, you have already done this), that would be

drwxrwxrwx 2 eric eric 4,0K nov  9 00:49 fakefoldername/

The point to notice here is that if you used chmod 777 fakefoldername it changes the permission set only to the directory without touching any of the files/directories in fakefoldername (use chmod -R 777 fakefoldername). Another point to remember is that directories leading to the fakefoldername also need to have sufficient perms. For this particular case, check if /tmp folder has them, fix if necessary

sudo chmod 1777 tmp/

Also, as stated above, I would try to make another directory in /var directory and see how things are going then (an nice answer why to or not to choose /var/tmp over /tmp here).

For others: the SDK that is used is probably https://github.com/Mangopay/mangopay2-php-sdk

There might be some hints in the somewhat similar answers below:

Sweepstakes answered 8/11, 2015 at 23:31 Comment(0)
P
0

Most likely, your apache dont have permissions to write that folder, you have to be sure that you are creating that directory with same user (in your case, apache) and also chmod.

Peroneus answered 11/11, 2015 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.