Search code examples
phphtmlc9.io

How to make a button, upload image to c9 folder?


I was wondering if it was possible to make a button in an HTML page or PHP that has the function to upload an image to your c9 folder instead "img" to your database(phpMyAdmin).

Once it is uploaded you can also display the image you just uploaded. But i think I can sort this one out like:

<html>
    <img src='photo/'/>
</html>

//or

<?php echo "<img src='photo/'/>"?>

What I want to do with this is like filling a form in and have a photo with it as extra information.

edit:

I tried this code:

$target_dir = "photo/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

Once i clicked the button to upload it, it did not save the file in the folder.

edit:

It works now, had to do the php.ini setup in c9.

P.S: I just started programming and would like to learn. If I make my question unclear please tell me how I can improve my question to be more accurate. :D


Solution

  • Refer this and this is a simple question,before ask the this type of question please search and study from internet.

    <?php
       if(isset($_FILES['image'])){
          $errors= array();
          $file_name = $_FILES['image']['name'];
          $file_size =$_FILES['image']['size'];
          $file_tmp =$_FILES['image']['tmp_name'];
          $file_type=$_FILES['image']['type'];
          $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
          
          $extensions= array("jpeg","jpg","png");
          
          if(in_array($file_ext,$extensions)=== false){
             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
          }
          
          if($file_size > 2097152){
             $errors[]='File size must be excately 2 MB';
          }
          
          if(empty($errors)==true){
             move_uploaded_file($file_tmp,"images/".$file_name);
             echo "Success";
          }else{
             print_r($errors);
          }
       }
    ?>
    <html>
       <body>
          
          <form action="" method="POST" enctype="multipart/form-data">
             <input type="file" name="image" />
             <input type="submit"/>
          </form>
          
       </body>
    </html>