PHP | בעיה עם סקריפט הורדת קובץ
שלום,
אני עובד כרגע על סקריפט אשר יאפשר הורדת קבצים, הסקריפט הולך ככה:
המשתנה $file מחזיק את הPath אל הקובץ, כולל שם הקובץ.
$fileDB הוא מופע של מחקלת הקבצים, הוא יכל להחזיק את שם הקובץ, הMini Type של הקובץ ועוד.
זה קוד א' של ההורדה:
PHP קוד:
// Begin writing headers
header("Cache-Control:");
header("Cache-Control: public");
header("Content-Type: $fileDB->miniType");
// if your filename contains underscores, replace them with spaces
header('Content-Disposition: attachment; filename='.str_replace("_", " ", $fileDB->fileName));
header("Accept-Ranges: bytes");
$size = filesize($file);
header("Content-Length: $size");
//open the file
$fp = fopen($file,'r');
//start buffered download
while(!feof($fp)) {
//reset time limit for big files
set_time_limit(0);
print(fread($fp,102400));
flush();
sleep(1);
}
fclose($fp);
זה קוד ב' של ההורדה:
PHP קוד:
// Begin writing headers
header("Cache-Control:");
header("Cache-Control: public");
header("Content-Type: $fileDB->miniType");
// if your filename contains underscores, replace them with spaces
header('Content-Disposition: attachment; filename='.str_replace("_", " ", $fileDB->fileName));
header("Accept-Ranges: bytes");
$size = filesize($file);
header("Content-Length: $size");
readfile($file);
הבעיה שלי היא שקוד א' לא 'מייצר' קבצים שעובדים, למשל אם אני מנסה להוריד תמונה, אי אפשר לצפות בה. לעומת זאת קוד ב' עושה את העבודה כמו שצריך.
אני צריך לגרום לקוד א' לעבוד כי הוא יותר גמיש ומאפשר המון דברים.
תודה מראש לעוזרים
|