1

我正在使用 Fineuploader 进行更改用户个人资料图片的 ajax 上传,我的 upload.php 文件的一部分如下:

function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
    if (!is_writable($uploadDirectory)){
        return array('error' => "Server error. Upload directory isn't writable.");
    }

    if (!$this->file){
        return array('error' => 'No files were uploaded.');
    }

    $size = $this->file->getSize();

    if ($size == 0) {
        return array('error' => 'File is empty');
    }

    if ($size > $this->sizeLimit) {
        return array('error' => 'File is too large');
    }

    $pathinfo = pathinfo($this->file->getName());
    $filename = $pathinfo['filename'];
    //$filename = md5(uniqid());
    $ext = @$pathinfo['extension'];     // hide notices if extension is empty

    if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
        $these = implode(', ', $this->allowedExtensions);
        return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
    }

    $ext = ($ext == '') ? $ext : '.' . $ext;

    if(!$replaceOldFile){
        /// don't overwrite previous files that were uploaded
        while (file_exists($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)) {
            $filename .= rand(10, 99);

        }
    }

    $this->uploadName = $filename . $ext;

    if ($this->file->save($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)){
        return array('success'=>true);

    } else {
        return array('error'=> 'Could not save uploaded file.' .
            'The upload was cancelled, or server error encountered');
    }

}    

因此,如果图像以相同的名称上传,它会创建一个带有数字的副本,所以如果 asd.jpg 上传两次,它的 asd44.jpg .. 但我找不到如何创建变量来获取带有随机数的图像路径创建的。

仅上传文件的初始名称。有谁知道如何执行此操作,然后将更新语句发送到数据库?

4

0 回答 0