title: Copy() function getting error in upload image with php code
body: <p>I'm having an unsolvable problem with uploading images to the server using php code. The code below is the file that uploads my images to the folder in the hosting, and saves the path to the database. I am using apache2 and php 7.4 with mysql. When I select the image to upload to the hosting directory, the</p>
  <pre><code>copy($_FILES['cons_image']['tmp_name'], $consname); 
  </code></pre>
  <p>do not copy the file to the directory on the hosting, and will lead to an error in this function.</p>
  <pre><code>if (!$copied) {
     echo '&lt;h1&gt;Copy unsuccessful!&lt;/h1&gt;';
     $errors = 1;
    }
  </code></pre>
  <p>I tried everything to solve the problem, and people said that I installed the missing php_gd library, but when I checked the php.ini file I already installed GD, used the phpinfo.php file to check the library. GD, everything is on. But when I check the php.ini file in the paragraph</p>
  <pre><code> [gd] 
  ; Tell the jpeg decode to ignore warnings and try to create 
  ; a gd image. The warning will then be displayed as notices ; disabled by
   default ; http://php.net/gd.jpeg-ignore-warning
   ;gd.jpeg_ignore_warning = 1
  </code></pre>
  <p>I don't see it having extension=gd line like in xampp on local machine. Can you help me check why the copy() function doesn't copy the images to the folder on my hosting. It still writes the path to the database but cannot copy the images to the hosting folder. I have included my code below hope anyone can help!</p>
  <pre><code>&lt;?php
  set_time_limit(0);
  
  include './controllers/config.php';
  
  //define a maxim size for the uploaded images
  define(&quot;MAX_SIZE&quot;, &quot;500&quot;);
  // define the width and height for the thumbnail
  // note that these dimensions are considered the maximum dimension and are not fixed, 
  // because we have to keep the image ratio intact or it will be deformed
  define(&quot;WIDTH&quot;, &quot;187&quot;);  //set here the width you want your thumbnail to be
  define(&quot;HEIGHT&quot;, &quot;105&quot;);  //set here the height you want your thumbnail to be.
  
  // this is the function that will create the thumbnail image from the uploaded image
  // the resize will be done considering the width and height defined, but without deforming the image
  function make_thumb($img_name, $filename, $new_w, $new_h) {
  //get image extension.
      $ext = getExtension($img_name);
  //creates the new image using the appropriate function from gd library
      if (!strcmp(&quot;jpg&quot;, $ext) || !strcmp(&quot;jpeg&quot;, $ext))
          $src_img = imagecreatefromjpeg($img_name);
  
      if (!strcmp(&quot;png&quot;, $ext))
          $src_img = imagecreatefrompng($img_name);
  
      if (!strcmp(&quot;gif&quot;, $ext))
          $src_img = imagecreatefromgif($img_name);
  
  //gets the dimensions of the image
      $old_x = imageSX($src_img);
      $old_y = imageSY($src_img);
  
      // next we will calculate the new dimensions for the thumbnail image
  // the next steps will be taken: 
  //  1. calculate the ratio by dividing the old dimensions with the new ones
  //  2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
  //      and the height will be calculated so the image ratio will not change
  //  3. otherwise we will use the height ratio for the image
  // as a result, only one of the dimensions will be from the fixed ones
      $ratio1 = $old_x / $new_w;
      $ratio2 = $old_y / $new_h;
      if ($ratio1 &gt; $ratio2) {
          $thumb_w = $new_w;
          $thumb_h = $old_y / $ratio1;
      } else {
          $thumb_h = $new_h;
          $thumb_w = $old_x / $ratio2;
      }
  
  // we create a new image with the new dimensions
      $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
  // resize the big image to the new created one
      imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
  
  // output the created image to the file. Now we will have the thumbnail into the file named by $filename
      if (!strcmp(&quot;png&quot;, $ext))
          imagepng($dst_img, $filename);
      else
          imagejpeg($dst_img, $filename);
  
      if (!strcmp(&quot;gif&quot;, $ext))
          imagegif($dst_img, $filename);
  
  //destroys source and destination images. 
      imagedestroy($dst_img);
      imagedestroy($src_img);
  }
  
  // This function reads the extension of the file. 
  // It is used to determine if the file is an image by checking the extension. 
  function getExtension($str) {
      $i = strrpos($str, &quot;.&quot;);
      if (!$i) {
          return &quot;&quot;;
      }
      $l = strlen($str) - $i;
      $ext = substr($str, $i + 1, $l);
      return $ext;
  }
  
  // This variable is used as a flag. The value is initialized with 0 (meaning no error found) 
  //and it will be changed to 1 if an error occurs. If the error occurs the file will not be uploaded.
  $errors = 0;
  // checks if the form has been submitted
  $error_message = &quot;&quot;;
  $sucess_message = &quot;&quot;;
  if (isset($_POST['Submit'])) {
      //reads the name of the file the user submitted for uploading
  
      $image = $_FILES['cons_image']['name'];
      $code_img = $_POST['code'];
  
  
      //// check code input
      if (empty($code_img)) {
          $error_message = &quot;Hãy điền mã cho hình ảnh, không được trùng mã của nhau!&quot;;
      } else {
          $checkDup = &quot;SELECT count(*) FROM uploads WHERE codes = :codes&quot;;
          $dupStmt = $sqlCon-&gt;prepare($checkDup);
          $dupStmt-&gt;bindValue(':codes', $code_img);
          $dupStmt-&gt;execute();
          $counts = $dupStmt-&gt;fetchColumn();
          if ($counts &gt; 0) {
              $error_message = &quot;Mã đã tồn tại, hãy chọn mã khác!&quot;;
          } else {
  
  
  
  
  // if it is not empty
              if ($image) {
  // get the original name of the file from the clients machine
                  $filename = stripslashes($_FILES['cons_image']['name']);
  
  // get the extension of the file in a lower case format
                  $extension = getExtension($filename);
                  $extension = strtolower($extension);
  // if it is not a known extension, we will suppose it is an error, print an error message 
  //and will not upload the file, otherwise we continue
                  if (($extension != &quot;jpg&quot;) &amp;&amp; ($extension != &quot;jpeg&quot;) &amp;&amp; ($extension != &quot;png&quot;) &amp;&amp; ($extension != &quot;gif&quot;)) {
                      $error_message = &quot;Tệp không rõ! chỉ tải lên duy nhất tệp .gif, .jpg hoặc .png !&quot;;
                      $errors = 1;
                  } else {
  // get the size of the image in bytes
  // $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which 
  //the uploaded file was stored on the server
                      $size = getimagesize($_FILES['cons_image']['tmp_name']);
                      $sizekb = filesize($_FILES['cons_image']['tmp_name']);
  
  //compare the size with the maxim size we defined and print error if bigger
                      if ($sizekb &gt; MAX_SIZE * 56024) {
                          $error_message =  &quot;Dung lượng giới hạn 5MB cho mỗi tệp!&quot;;
                          $errors = 1;
                      }
  
  
                      $rand = rand(0, 1000);
  //we will give an unique name, for example a random number
                      $image_name = $rand . '.' . $extension;
  //the new name will be containing the full path where will be stored (images folder)
                      $consname = &quot;thumb/&quot; . $image_name;  //change the image/ section to where you would like the original image to be stored
                      $consname2 = &quot;thumb/thumbs/&quot; . $image_name; //change the image/thumb to where you would like to store the new created thumb nail of the image
                      $copied = copy($_FILES['cons_image']['tmp_name'], $consname);
                      $copied = copy($_FILES['cons_image']['tmp_name'], $consname2);
  
                      $sql = &quot;INSERT INTO uploads(thumb, small_thumb, codes) VALUE(:thumb, :images, :codes) &quot;;
                      $statement = $sqlCon-&gt;prepare($sql);
                      $statement-&gt;bindValue(':thumb', $consname2);
                      $statement-&gt;bindValue(':images', $consname);
                      $statement-&gt;bindValue(':codes', $code_img);
                      $count = $statement-&gt;rowCount();
                      $statement-&gt;execute();
                      $statement-&gt;closeCursor();
                      $sucess_message = &quot;Tải lên hình ảnh thành công!&quot;;
  
  //we verify if the image has been uploaded, and print error instead
                      if (!$copied) {
                          echo '&lt;h1&gt;Copy unsuccessful!&lt;/h1&gt;';
                          $errors = 1;
                      } else {
  // the new thumbnail image will be placed in images/thumbs/ folder
                          $ori_image = $consname;
                          $thumb_name = $consname2;
  // call the function that will create the thumbnail. The function will get as parameters 
  //the image name, the thumbnail name and the width and height desired for the thumbnail
                          $thumb = make_thumb($consname, $thumb_name, WIDTH, HEIGHT);
                      }
                  }
              }
          }
      }
  }
  
  //If no errors registered, print the success message and how the thumbnail image created
  //if (isset($_POST['Submit']) &amp;&amp; !$errors) {
  //   $error_message = &quot;Tải lên hình ảnh thành công!&quot;;
  //}
  //echo &quot;&lt;form name=\&quot;newad\&quot; method=\&quot;post\&quot; enctype=\&quot;multipart/form-data\&quot;  action=\&quot;\&quot;&gt;&quot;;
  //echo &quot;&lt;input type=\&quot;file\&quot; name=\&quot;cons_image\&quot;  &gt;&quot;;
  //echo &quot;&lt;input name=\&quot;Submit\&quot; type=\&quot;submit\&quot;  id=\&quot;image1\&quot; value=\&quot;Upload image\&quot; /&gt;&quot;;
  //echo &quot;&lt;/form&gt;&quot;;
  // lay tat ca hinh anh len trang
  $selectImg = &quot;SELECT * FROM uploads ORDER BY id DESC&quot;;
  $stmtImg = $sqlCon-&gt;prepare($selectImg);
  $stmtImg-&gt;execute();
  $imgList = $stmtImg-&gt;fetchAll();
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset=&quot;UTF-8&quot;&gt;
          &lt;title&gt;Thiên Long Huyết Tử&lt;/title&gt;
          &lt;link rel=&quot;icon&quot; type=&quot;image/png&quot; href=&quot;imgs/favicon.ico&quot;/&gt;
          &lt;link rel=&quot;stylesheet&quot; href=&quot;ht-admin/css/main.min.css&quot;&gt;
          &lt;link href=&quot;http://tl-huyettu.us/css/aos.css&quot; rel=&quot;stylesheet&quot;&gt;
          &lt;script src=&quot;http://tl-huyettu.us/js/aos.js&quot;&gt;&lt;/script&gt;
  
          &lt;link href=&quot;http://tl-huyettu.us/ht-admin/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; id=&quot;bootstrap-css&quot;&gt;
          &lt;script src=&quot;http://tl-huyettu.us/ht-admin/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
          &lt;script src=&quot;http://tl-huyettu.us/ht-admin/js/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;
  
      &lt;/head&gt;
      &lt;body style=&quot;background-color: #2b3035;&quot;&gt;
  
          &lt;?php include './ht-admin/pages/NarvigationBar.php'; ?&gt;
  
          &lt;!-- nav bar --&gt;
  
          &lt;!-- end nav bar ---&gt;
          &lt;?php include './ht-admin/pages/verticleBar.php'; ?&gt;
  
          &lt;div class=&quot;form-upload&quot;&gt;
              &lt;form name=&quot;newad&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;&quot;&gt;
                  &lt;span&gt;&lt;?php echo $error_message; ?&gt;&lt;/span&gt;
                  &lt;h5&gt;&lt;?php echo $sucess_message; ?&gt;&lt;/h5&gt;
                  &lt;br&gt;&lt;br&gt;
  
                  &lt;lable&gt;Code&lt;/lable&gt;
                  &lt;input class=&quot;code&quot; name=&quot;code&quot; type=&quot;text&quot; placeholder=&quot;Hãy tạo mã cho hình ảnh, không được trùng nhau...&quot;&gt;&lt;br&gt;
                  &lt;div class=&quot;file-image&quot;&gt;
                      &lt;lable&gt;Hình ảnh&lt;/lable&gt;
                      &lt;input  type=&quot;file&quot; name=&quot;cons_image&quot;&gt;
                  &lt;/div&gt;
                  &lt;input class=&quot;button-s&quot; type=&quot;submit&quot; name=&quot;Submit&quot; id=&quot;image1&quot; value=&quot;Upload&quot;&gt;
  
                  &lt;div class=&quot;thumbnails&quot;&gt;
                      &lt;label&gt;Thumbnails&lt;/label&gt;
                      &lt;img src=&quot;&lt;?php echo $thumb_name; ?&gt;&quot; width=&quot;150&quot; height=&quot;70&quot; /&gt;&lt;br&gt;
                      &lt;label&gt;Original Image&lt;/label&gt;
                      &lt;img src=&quot;&lt;?php echo $ori_image; ?&gt;&quot; width=&quot;350&quot; height=&quot;175&quot; /&gt;
                  &lt;/div&gt;
  
              &lt;/form&gt;
  
              &lt;div class=&quot;image-panel-list&quot;&gt;
                  &lt;table&gt;
                      &lt;tbody class=&quot;body-image-list&quot; &gt;
                          &lt;?php foreach ($imgList as $lits): ?&gt;
                              &lt;tr&gt;
                                  &lt;td&gt;&lt;img src=&quot;&lt;?php echo $lits['thumb'] ?&gt;&quot; width=&quot;150&quot; height=&quot;70&quot; /&gt;&lt;/td&gt;
                                  &lt;td class=&quot;td-code-list&quot; &gt;&lt;?php echo $lits['codes'] ?&gt;&lt;/td&gt;
  
                              &lt;/tr&gt;
  
                          &lt;?php endforeach; ?&gt;
                      &lt;/tbody&gt;
  
  
                  &lt;/table&gt;
              &lt;/div&gt;
  
          &lt;/div&gt;
  
          &lt;!-- tabs image script --&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>