php: تغییر نام فایل قبل از اپلود

بازدید43.9kپست ها1آخرین فعالیت9 سال پیش
0
0

سلام
چطور باید قبل از اپلود فایل اسم فایل رو تغییر بدیم؟

0

سلام یه کد گذاشتم که همین کارو انجام میده تست کن:

define("UPLOAD_DIR", "/path/to/uploads/");

if (!empty($_FILES["ambum_art"])) 
{
    // The file uploaded
    $myFile = $_FILES["ambum_art"];

    // Check there was no errors
    if ($myFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }

    // Rename the file so it only contains A-Z, 0-9 . _ -
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

    // Split the name into useful parts
    $parts = pathinfo($name);

    // This part of the code should continue to loop until a filename has been found that does not already exist.
    $i = 0;
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }

    // If you want to set a random unique name for the file then uncomment the following line and remove the above
    // $name = uniqid() . $parts["extension"];

    // Now its time to save the uploaded file in your upload directory with the new name
    $success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR . '/'.$name);

    // If saving failed then quit execution
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    }

    // Set file path to the $Dir variable
    $Dir = UPLOAD_DIR .'/'. $name;

    // Set the permissions on the newly uploaded file
    chmod($Dir, 0644);

    // Your application specific session stuff
    unset($_SESSION['video']);
    $_SESSION['album_art'] = $Dir; // Save the file path to the session
    $ambum_art_result = array();
    $ambum_art_result['content'] = $Dir;
    echo json_encode($ambum_art_result);

}

سوال برنامه نویسی دارید؟

ندونستن عیب نیست، نپرسیدن چرا!

خوش آمدید

برای طرح سوال، ایجاد بحث و فعالیت در سایت نیاز است ابتدا وارد حساب کاربری خود شوید. در صورتی که هنوز عضو سایت نیستید میتوانید در عرض تنها چند ثانیه ثبت نام کنید.