-1

I have a FORM:

 <form method="post" action="">
    <table class="table table-bordered">
        <tr>
            <td>
                الصورة
            </td>
            <td>
                <input type="file" class="form-control" name="img">
            </td>
        </tr>
        <tr>
            <td>
                العنوان
            </td>
            <td>
                <input type="text" class="form-control" name="headline">
            </td>
        </tr>
        <tr>
            <td>
                العنوان المميز
            </td>
            <td>
                <input type="text" class="form-control" name="special_headline">
            </td>
        </tr>
        <tr>
            <td>
                التفاصيل
            </td>
            <td>
                <input type="text" class="form-control" name="details">
            </td>
        </tr>
        <tr>
            <td>
                رابط الزر
            </td>
            <td>
                <input type="text" class="form-control" name="button_link">
            </td>
        </tr>
        <tr>
            <td>
                نص الزر

            </td>
            <td>
                <input type="text" class="form-control" name="button_text">
            </td>
        </tr>
        <tr>
            <td>

            </td>
            <td>
                <input type="submit" class="btn btn-success" name="new" value="حفظ">
            </td>
        </tr>

    </table>
</form>

And this is my PHP code to process this form and upload the image:

    if($_POST['new'])
    {
        $maxsize= 52428800;
        $alemtdadat = array('.jpg');
        $upload_path = 'uploads';
        $file_name = $_FILES['img']['name'];
        $fileup = $_FILES['img']['tmp_name'];
         // نقوم بإنتاج رقم عشوائي لكي نضيفه إلى إسم الملف لكي لاتكرر اسماء الملفات في
         // مجلد الرفع
         $rand = rand(000,999);
         // نقوم بوضع متغير يحتوي على اسم الملف ونلاحظ اننا اضفنا متغير اسم الملف العشوائي
         $new_file_name=$rand.'_'.$file_name;

        $headline = $_POST['headline'];
        $special_headline = $_POST['special_headline'];
        $details = $_POST['details'];
        $button_link = $_POST['button_link'];
        $button_text = $_POST['button_text'];

 // إذا كان لم يتم اختيار ملف
 if($fileup == ''){
?>
<div class="alert alert-danger">أنت لم تقم بإختيار ملف لرفعه !</div>

<?php
 }else{

  // نحضر إمتداد الملف
  $att = strtolower(strrchr($file_name,'.'));

   // إحضار حجم الملف بواسطة الدالة
  // filesize
  // مع ملاحظة ان هذه الدالة تحضر حجم الملف بالبايت
  $file_size = filesize($fileup);

  // التأكد من أن الإمتداد موجود في مصفوفة الإمتدادات
  if(! in_array($att,$alemtdadat)){
?>
<div class="alert alert-danger">عفوًا يجب أن يكون المقطع بإمتداد jpg فقط !</div>

<?php
 }

   // التأكد من حجم الملف
  elseif($file_size > $maxsize)
  {
   // نقوم بإحضار حجم الملف بالكيلوبايت لكي نقوم بطباعته للزائر
   $max_size = ($maxsize/1024);
?>
<div class="alert alert-danger">حجم الملف تجاوز الحجم الأقصى !</div>

<?php

  }else{
 // وضعنا متغير يحتوي على مسار مجلد الرفع ثم اسم الملف لكي نحدد المسار كاملاً
 $path= $upload_path.'/'.$new_file_name;

  // رفع الملف
  // هنا استخدمنا الدالة move_uploaded_file
  // ولهذه الداله بارامتاران
 // الأول الملف , والثاني مسار الملف وقد حددنا في أعلى
  $Upload = move_uploaded_file($fileup,$path);

   // إذا تم رفع الملف
   if($Upload)
    {


        $add = $slider->add($img,$headline,$special_headline,$details,$button_link,$button_text);
                        if($add)
                        {
                            echo "EVET";
                        }
                        else
                        {
                            echo "YOK";
                        }

                 $size=  ($_FILES['img']['size']/1024);
                // نحضر نوع الملف
                 $type = $_FILES['img']['type'];
                 // نطبع القالب المسؤول عن عرض معلومات الملف المرفوع
?>
<div class="alert alert-success">تم الرفع بنجاح !</div>

<?php

    }else{
?>
<div class="alert alert-danger">يوجد خطأ !</div>

<?php

              }

       }
  }



}

The Problem that the output is:

أنت لم تقم بإختيار ملف لرفعه !

It's the result of this condition:

$fileup = $_FILES['img']['tmp_name'];
if($fileup == '')
Braiam
  • 1
  • 11
  • 47
  • 78
  • 3
    make sure you have `
    – Kevin May 02 '15 at 11:34
  • possible duplicate of [Upload Image to Server using PHP. Store file name in a MYSQL database, with other profile info](http://stackoverflow.com/questions/450876/upload-image-to-server-using-php-store-file-name-in-a-mysql-database-with-othe) – Jayson May 02 '15 at 11:38

1 Answers1

0

You should use enctype attribute in the form tag. Modify form tag like this:

<form method="post" action="" enctype="multipart/form-data">

This should work in the case of image upload. enctype attribute specify the content type when submitting the data.

Tapan Prakash
  • 773
  • 8
  • 7