PDA

View Full Version : سوال: محدودیت برای آپلود فایل



soroush.r70
دوشنبه 26 دی 1390, 14:29 عصر
دوستان من یه فرم آپلود فایل دارم می خوام براش محدودیت زارم مثل اینکه اگه حجمش از 3MB بیشتر بود پیغام بده و فقط فرمت هایی که من می گم آپلود بشه در غیر این صورت پیغام خطا بده.

حجم فایل = 3MB در غیر این صورت پیغام خطا و آپلود نشود.

نوع فایل مشخص = zip - rar - pdf - jpg - bmp - gif - doc در غیر این صورت خطا و فایل آپلود نشود.

djsaeedkhan
دوشنبه 26 دی 1390, 14:39 عصر
با سلام

if ($_FILES["file"]["size"] < 220000)
{
if ($_FILES["file"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
//$nam= $_FILES["file"]["type"];
//echo $nam;
if (file_exists("../picture/" . $_FILES["file"]["name"]))
{
echo($_FILES["file"]["name"] . " already exists. ");
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "../picture/" . $_FILES["file"]["name"]);

djsaeedkhan
دوشنبه 26 دی 1390, 14:40 عصر
<?php
// begin Dave B's Q&D file upload security code
$allowedExtensions = array("txt","csv","htm","html","xml",
"css","doc","xls","rtf","ppt","pdf","swf","flv","avi",
"wmv","mov","jpg","jpeg","gif","png");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions)) {
die($file['name'].' is an invalid file type!<br/>'.
'<a href="javascript:history.go(-1);">'.
'&lt;&lt Go Back</a>');
}
}
}
// end Dave B's Q&D file upload security code
?>

djsaeedkhan
دوشنبه 26 دی 1390, 14:41 عصر
<?php

$file = $_FILES['userfile'];

$allowedExtensions = array("txt", "rtf", "doc");

function isAllowedExtension($fileName) {
global $allowedExtensions;

return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

if($file['error'] == UPLOAD_ERR_OK) {
if(isAllowedExtension($file['name'])) {
# Do uploading here
} else {
echo "Invalid file type";
}
} else die("Cannot upload");

?>

djsaeedkhan
دوشنبه 26 دی 1390, 14:42 عصر
<?php
// Configuration - Your Options
$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');

// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');

// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed :(.

?>

MMSHFE
دوشنبه 26 دی 1390, 16:22 عصر
با سلام، این کد رو تست کنید. امیدوارم به درد بخوره:


if(isset($_FILES['photo'])) {
$photo = &$_FILES['photo'];
$ext = substr($photo['name'], -3);
$exts = array('doc', 'gif', 'bmp', 'jpg', 'pdf', 'rar', 'zip');
if($photo['size'] > 3145728) {
echo 'Size error (must be < 3MB)<br/>'."\n";
}
elseif(in_array($ext, $exts)) {
move_uploaded_file($photo['tmp_name'], 'images/'.$photo['name']);
}
else {
echo 'Invalid file (only ';
foreach($exts as $ext) {
echo '".'.$ext.'" ';
}
echo 'extensions are allowed<br/>'."\n";
}
}