tempnam() not working on client server

I did a script, which has to load a file from a ftp-server then parses it. The code relies on tempname to store the temporarily store the ftp-file. On my developement server (with php 5.3.10), this works flawlessly, however on the client machine (with php 5.2.17) it does not and gives me:

Warning: ftp_rawlist() [function.ftp-rawlist]: Unable to create temporary file. Check permissions in temporary files directory.

Can someone give me a clue what i could do? (I am a little weak on the possibiities of php)

I used this code:

define ("LOCALFILE",tempnam('/tmp', 'data-'));
define ("USER","myusername");
define ("PASS","mypassword");
define('SERVER', "ftpserver.com");
define ("DIR","/path/");

function getFTPFile(){

    // connect
    if(!($conn_id = @ftp_connect(SERVER))){
        Error::throwOne("Could not connect to ".SERVER);

    };
    // login 
    if(!($login_result = @ftp_login($conn_id, USER, PASS))){
        Error::throwOne("LOGIN INCORRECT! user:".USER." pass:".PASS);
    };
    // try to change the directory to somedir
    if (!ftp_chdir($conn_id, DIR)) {
        Error::throwOne("Couldn't change directory\n");
    }; 

    if(!($a = ftp_rawlist($conn_id, '-1t'))){
        Error::throwOne("Couldn't get ftp_rawlist\n");
    };

    $server_file=($a[0]);
    if (!ftp_get($conn_id, LOCALFILE, $server_file, FTP_BINARY)) {
        Error::throwOne("Couldn't get file\n");
    };
    ftp_close($conn_id);    
};

Answers


The error looks to me like system-generated and have nothing to do with your tmpname. Internally, FTP stores the file in its own temp file in temp filename in standard temp directory up to the point when it's downloaded - then moves it to the location you specified.

Try putting this code before your download code:

$tmpdir = sys_get_temp_dir();
echo "Temp dir: $tmpdir\n";
echo  is_writable($tmpdir) ? "Temp dir is writable" : "Temp dir is not writable";

This will tell you if you have permissions to write to the system temp dir. I further suggest that you use this $tmpdir variable instead of hardcoding "/tmp" in your code.


I found the issue. Since my hoster does not like the /tmp directory ( who would blame them ) i need to first set putenv('TMPDIR=/the tmp-dir my provider gave me'); which then works with tempname


Need Your Help

Get Folders WITHOUT documents

php mysql

I use this query to get the number of documents the folder contains:

Compiled Linq & String.Contains

.net linq linq-to-sql

I'm using Linq-to-SQL and I use compiled Linq for better performance.