When I upload an Image in PHP there's a condition that always true

Ahmed Essam

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 == '')
Tapan Prakash

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is this PHP if condition always true?

From Dev

Why is this PHP if condition always true?

From Dev

Condition always true when reached in while loop

From Dev

I recieve a white page when the second condition is true in php

From Dev

Php multiple image upload fails when i select many images

From Dev

if condition always evaluating true

From Dev

Term for an if condition that is always true

From Dev

PHP if statement always TRUE when comparing strings

From Java

Boolean condition is always false when using `status == true`

From Dev

passport's req.isAuthenticated always returning false, even when I hardcode done(null, true)

From Dev

if condition always returns true (Java)

From Dev

awk condition always TRUE in a loop

From Dev

Bash Script if condition is always true

From Dev

get the name of image when i upload it

From Dev

get the name of image when i upload it

From Dev

Error Apache when I upload a Image

From Dev

PHP resize an image and upload to s3

From Dev

PHP resize an image and upload to s3

From Dev

How do I upload an image using PHP?

From Dev

Why cant i upload image on Database in php?

From Dev

HasOneOrMany.php line 221 error in laravel when I try to upload an image

From Dev

i want to resize a profile image when the user upload it using PHP and ImageManipulator library

From Dev

Trying to upload files to the server with PHP but it's always converted as .jpg format

From Dev

Always TRUE result in PHP code when delete query is used

From Dev

PHP add element to array only when condition is true

From Dev

How can I return true when the condition of where clause is false?

From Dev

How do I return data when the condition of two statements are true?

From Dev

strpos function is not working correctly when i use true in condition why?

From Dev

Angular 2 - Custom validator always returns true, even when condition is false

Related Related

  1. 1

    Why is this PHP if condition always true?

  2. 2

    Why is this PHP if condition always true?

  3. 3

    Condition always true when reached in while loop

  4. 4

    I recieve a white page when the second condition is true in php

  5. 5

    Php multiple image upload fails when i select many images

  6. 6

    if condition always evaluating true

  7. 7

    Term for an if condition that is always true

  8. 8

    PHP if statement always TRUE when comparing strings

  9. 9

    Boolean condition is always false when using `status == true`

  10. 10

    passport's req.isAuthenticated always returning false, even when I hardcode done(null, true)

  11. 11

    if condition always returns true (Java)

  12. 12

    awk condition always TRUE in a loop

  13. 13

    Bash Script if condition is always true

  14. 14

    get the name of image when i upload it

  15. 15

    get the name of image when i upload it

  16. 16

    Error Apache when I upload a Image

  17. 17

    PHP resize an image and upload to s3

  18. 18

    PHP resize an image and upload to s3

  19. 19

    How do I upload an image using PHP?

  20. 20

    Why cant i upload image on Database in php?

  21. 21

    HasOneOrMany.php line 221 error in laravel when I try to upload an image

  22. 22

    i want to resize a profile image when the user upload it using PHP and ImageManipulator library

  23. 23

    Trying to upload files to the server with PHP but it's always converted as .jpg format

  24. 24

    Always TRUE result in PHP code when delete query is used

  25. 25

    PHP add element to array only when condition is true

  26. 26

    How can I return true when the condition of where clause is false?

  27. 27

    How do I return data when the condition of two statements are true?

  28. 28

    strpos function is not working correctly when i use true in condition why?

  29. 29

    Angular 2 - Custom validator always returns true, even when condition is false

HotTag

Archive