quicksand
quicksand

Reputation: 33

uploading photos to server/rackspace cloudfiles - php/codeigniter

I'm working on uploading photos from my computer to the server, using this repository. When I try to upload a file called mylogo.jpg, I get an error that says:

Could not open file for reading: /tmp/php0d4X5Ny/mylogo.jpg

I need to figure out why I'm getting this error.

Here's my code from the controller (cloudfiles.php):

        public function add_local_file()
        {
                $file_location = $_FILES['userfile']['tmp_name'].'/';
                $file_name = $_FILES['userfile']['name'];

                $this->cfiles->do_object('a', $file_name, $file_location);

                $this->_show_errors('Image Added!');
        }

The do_object function is here (line 151). And here is the upload view:

<?php echo form_open_multipart('cloudfiles/add_local_file'); ?>

<input type="file" name="userfile" size="20" />

<input type="submit" value="upload" />

<?php echo form_close(); ?>

I searched the cloudfiles library and found that the "could not open file for reading" error is an exception in this function:

function load_from_filename($filename, $verify=true)
    {
        $fp = @fopen($filename, "r");
        if (!$fp) {
            throw new IOException("Could not open file for reading: ".$filename);
        }

        clearstatcache();

        $size = (float) sprintf("%u", filesize($filename));
        if ($size > MAX_OBJECT_SIZE) {
            throw new SyntaxException("File size exceeds maximum object size.");
        }

        $this->_guess_content_type($filename);

        $this->write($fp, $size, $verify);
        fclose($fp);
        return True;
    }

I've been looking at this for several hours and can't see what's going wrong. Not seeing any php errors, by the way, just the one error noted above. New to Stack Overflow (frequent browser, new account), so thanks in advance for your help.

Upvotes: 3

Views: 667

Answers (1)

keithhatfield
keithhatfield

Reputation: 3273

I don't think you understand how the $_FILES array works. Once the file is uploaded, it is located at $_FILES['userfile']['tmp_name'] and does not have it's original name.

If you want the file uploaded with it's original name, you will need to do something like:

public function add_local_file()
{
    $file_location = '/tmp/';
    $file_name     = $_FILES['userfile']['name'];

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $file_location . $file_name)){

        $this->cfiles->do_object('a', $file_name, $file_location);

    }

    $this->_show_errors('Image Added!');
}

Upvotes: 5

Related Questions