0

I want to upload images using PHP, with proper validation such as to check width and to replace white spaces with - or _ in filename.

Here is my code:

function ubimg($fileName, $fileType, $fileTmpName, $pathToSaveIn) {
    $flagUpload  = false;
    $rndFileName = "";
    $rndNum      = rand(10000000, 9899999999);
    if (($fileType == "image/gif") || ($fileType == "image/jpeg") || ($fileType == "image/png") && imagesx < 600) {
        $nameFile         = $rndNum . "_" . $fileName;
        $cfile            = preg_replace('/\s\s+/', "_", $nameFile);
        $_SESSION['bimg'] = $cfile;
        if (!file_exists($pathToSaveIn . "/" . $nameFile)) {
            move_uploaded_file($fileTmpName, $pathToSaveIn . "/" . $nameFile);
            $flagUpload = true;
        }
    }
    else {
        msgbox("error", $php_errormsg);
    }
    return $flagUpload; //may be return true or false
}

I have used preg_replace but its not working on removing spaces from file name???? any solution

4

1 に答える 1

0

これを使用して、文字列内の余分な空白を削除できます

$str= preg_replace( '/\s+/', ' ', $str); 

また

$str = str_replace(' ','',$str);
于 2012-09-24T17:30:12.890 に答える