الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في استخدام سكربت file upload

بدأه LibyanITGirl في 24 ديسمبر 2011 · 5 رد · 1,086 مشاهدة · في منتدى تطوير المواقع بـ PHP
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

قمت بكتابة كود registration form ... والكود يحتوي على سكربت لرفع ملف

لكن تواجهني مشكلة ظهور هذه الرسالة

"The file you attempted to upload is not allowed."

هذا هو الكود :

index.php

<html>
<body>
<center>
<h3>Registration</h3>
<form action="./upload.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>First Name<td><input name="firstname" type="text" /></td></br></td>
</tr>
<tr>
<td>Last Name<td><input name="lastname" type="text" /></td></br></td>
</tr>
<tr>
<td>e-mail<td><input name="email" type="text" /></td></br></td>
</tr>
<tr>
<td>Phone<td><input name="phone" type="text" /></td></br></td>
</tr>
<tr>
<td>Institution <td><input name="institution" type="text" /></td></br></td>
</tr>
<tr>
<td>Specialist <td><input name="specialist" type="text" /></td></br></td>
</tr>
<tr>
<td>Do you require accommodation?
<td><Input type = 'Radio' Name ='accommodation' value= 'Yes'>Yes
<Input type = 'Radio' Name ='accommodation' value= 'No'>No
</td>
</td></tr>
<tr>
<td>Grade
<td><select name="grade">
<option value="student">Student</option>
<option value="intern">Intern</option>
<option value="senior">Senior House Officer</option>
<option value="registrar">Registrar</option>
<option value="specialist">Specialist</option>
<option value="consultant">Consultantant</option…
</select></td>
</td></tr>
<tr>
<td>Participation Type
<td> 
<input type="checkbox" name="P" value="Paper" />Paper</br>
<input type="checkbox" name="W" value="Workshop" />Workshop</br>
<input type="checkbox" name="Pr" value="Presentation" />Presentation</br>
<input type="checkbox" name="A" value="Attendance" />Attendance</td>
</td>
</tr>
<tr>
<td>
</br>
<label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> 


</br>
</br>
</td>
</tr>
<tr>
<td>
<input type="Submit">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>

upload.php

<?php

// Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png','.docx','.txt'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './uploads/'; // 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).
   $filename = time() .'_'. $filename;
   $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 :(.




// Get values from form 
$firstname=$_POST['lastname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$institution=$_POST['institution'];
$specialist=$_POST['specialist'];
$accommodation=$_POST['accommodation'];
$grade=$_POST['grade'];
$P=$_POST['P'];
$W=$_POST['W'];
$Pr=$_POST['Pr'];
$A=$_POST['A'];


// Insert data into mysql 
$link=mysql_connect ("localhost","root","123456") or die ("Connection Failed !!");
echo "<br>";  
echo "<br>";  
if((firstname && lastname && email && Institution && Specialist)<>""){
$sql = 'INSERT INTO `jml-reg` (`id`, `firstname`, `lastname`, `email`, `phone`, `institution`, `specialist`, `accommodation`, `grade`, `paper`, `workshop`, `presentation`, `attendance`) VALUES (NULL, \'trt\', \'dfdf\', \'fdfd\', \'545\', \'dfdf\', \'fdfd\', \'Yes\', \'sfd\', \'dsds\', \'dsd\', \'dsd\', \'dsds\');';
mysql_query($sql); 
mysql_close(); 
}

?>

قمت برفع الملفات المسموحة ولكن تظهر لي هذه الرسالة

lilgirllaptopsoftened2c.jpg

#2
   // 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.');

يستبدل بهذا الكود

  // 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.');

صورة
صورة

حسابي في الفيس بوك
http://goo.gl/XIzwL

حسابي في تويتر
http://goo.gl/6p4e3

 

 
 
#3

بارك الله فيك

الكود شغال الآن ... مع أني لم أغير شئ !!

lilgirllaptopsoftened2c.jpg

#4

المشكلة كانت ال cache في المتصفح !

lilgirllaptopsoftened2c.jpg

#5

واجهتني مشكلة أخرى

هي عند عدم إرسال اي ملف تظهر لي نفس الرسالة

أريد وضع شرط if statement انه اذا لم يتم إرسال اي ملف يتم تجنب جميع الجمل الشرطية التي تقوم بفحص الملف

قمت بعمل جملة شرطية .. لكن لم يكن لها تأثير !

<?php

   // Configuration - Your Options
      $allowed_filetypes = array('.jpg','.gif','.bmp','.png','.docx','.txt','.doc','.ppt','.pptx','.rar','.zip','.pdf'); // These will be  the types of file that will pass the validation.
      $max_filesize = 200524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './uploads/'; // 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).
   $filename = time() .'_'. $filename;
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

if ($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 '<center>Your file upload was successful</center>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.

}

// Get values from form 
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$institution=$_POST['institution'];
$specialist=$_POST['specialist'];
$country=$_POST['Country'];
$accommodation=$_POST['accommodation'];
$grade=$_POST["grade"];
$P=$_POST['Participation'];
$W=$_POST['W'];
$Pr=$_POST['Pr'];
$A=$_POST['A'];

// Insert data into mysql 
$link=mysql_connect ("*******","*****","*****") or die ("Connection Failed !!");
mysql_select_db ("******");


$result ="INSERT INTO `jml-reg` (  `id` ,  `firstname` ,  `lastname` ,  `email` ,  `phone` ,  `institution` ,  `specialist` ,   `country` , `accommodation` ,  `grade` ,  `paper` ,  `workshop` ,  `presentation` , `attendance` ,  `upload` ) VALUES ( NULL, '".$firstname."',  '".$lastname."',  '".$email."',  '".$phone."',  '".$institution."',  '".$specialist."',  '".$country."',  '".$accommodation."',  '".$grade."',  '".$Participation."',  '".$W."',  '".$Pr."',  '".$A."', '".$filename."')";

mysql_query($result); 
echo '<center><h3></br></br></br>Registration was successful ! </h3></center> '; 
?>

تم تعديل هذه المشاركة بواسطة LibyanITGirl في 27 ديسمبر 2011 في 21:39

lilgirllaptopsoftened2c.jpg

#6

ـم حل المشكلة !

استخدمت :

if (!empty($_FILES))
{

the checking process 

}

تم تعديل هذه المشاركة بواسطة LibyanITGirl في 28 ديسمبر 2011 في 12:08

lilgirllaptopsoftened2c.jpg

مواضيع مشابهة