Frage

I'm trying to upload file but get this error message :

 move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: The second argument to copy() function cannot be a directory 

I think there's something problem with this file but I've no idea to solve it..

    <?php


    class FileUploadController extends CController {


    public function actionUpload() {
        $model = new FileUpload();
    $form = new CForm('application.views.fileUpload.uploadForm', $model);
        if ($form->submitted('submit') && $form->validate()) {
            $form->model->image = CUploadedFile::getInstance($form->model, 'image');


                  if($model->validate())
            {
                $model->image->saveAs('/opt/lampp/htdocs/upl/images');

            Yii::app()->user->setFlash('success', 'File Uploaded');
            $this->redirect(array('upload'));
            }


        }
        $this->render('upload', array('form' => $form));
    }
}

?>
War es hilfreich?

Lösung

You either need to check all files existed at '/opt/lampp/htdocs/upl/images' and check if the same named file is available or not, if available then just rename the file with extra "_1" every time, or you can always upload the file by renaming the file into some machine name sort of thing see the code below,

$name = rand(1000,9999) . time(); // rand(1000,9999) optional
$name = md5($name); //optional
$model->image->saveAs('/opt/lampp/htdocs/upl/images/' . $name . '.jpg');

This is what I usually do with file uploads, provided that you're saving the files references into the database or in any text file.

EDIT

Get Extension.

In case if you're required to get extension of the file rather then of hard-coded, you can use $model->image->getExtensionName(); it will get you the extension of the uploaded file without . (dot)

Andere Tipps

Finally, I solved it by myself:

The problem was located in line

$model->image->saveAs('/opt/lampp/htdocs/upl/images');

It should be :

$model->image->saveAs('/opt/lampp/htdocs/upl/images/images.jpg');

Now, there's another problem: when I upload 'new image', it will be replace the old file, I want the file(s) being uploaded not replaced the old file or I need something like rename as new file. Does anyone knows?

goto cuploadedfile.php . over write this function with this

if($this->_error==UPLOAD_ERR_OK)
{
    if($deleteTempFile)
        return move_uploaded_file($this->_tempName,$file."/".$this->getName());
    elseif(is_uploaded_file($this->_tempName))
        return copy($this->_tempName, $file."/".$this->getName());
    else
        return false;
}

thank u.........

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top