1

So I've got an apache2 web server, and I've made it so it can run PHP files, but it does not seem to work at all.

My PHP code of image uploading

<?php
$target_dir = "~/pictures/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}


// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

My View Code

<!DOCTYPE html>
<html>
    <head>
        <title>  War Thunder Social </title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    </head>
    <body onload="setInterval('chat.update()', 1000)">
        <style>
            body {
                margin: 0;
                color: #f4976c;
                background: #fbe8a6;
                scrollbar-color: black;
                scroll-behavior: smooth;
                scroll-snap-type: mandatory;
                scrollbar-base-color: #303c6c;
                scrollbar-shadow-color: aquamarine;
            }
            .topnav {
                width: 100%;
                height: 50px;
                overflow: hidden;
                top: 0;
                position: sticky;
                background: #303c6c;
            }
            .topnav a {
                width: 10%;
                color: black;
                height: 50px;
                text-decoration: none;
                padding-top: 10px;
                text-align: center;
                float: left;
                background: #f4976c;
            }
            .home {
                width: 100%;
                margin-top: 0;
                background: #f4976c;
                height: 400px;
                clip-path: polygon(0 0, 100% 0, 100% 70%, 0 100%);
            }
            .home h1 {
                font-size: 800;
                transition: 0.5s;
                text-shadow:  -2px 1px 3px black;
            }
            .home h1:hover {
                transition: 0.5s;
                mix-blend-mode: color-dodge;
            }
            h1 {
                text-align: center;
                margin-top: 0;
                padding-top: 80px;
                font-size: 800;
                padding-left: 100px;
            }
            
        </style>
        <style>
        .parallax {
            height: 500px;
            background-attachment: fixed;
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
            scroll-behavior: smooth;
            scrollbar-color: black;
          }
          </style>
          
          
        <style>

.prev {
    width: 100%;
    height: 800px;
    background: #b4d5e5;
    text-rendering: optimizeLegibility;
}

    p {
        padding: 10px;
        padding-left: 30px;
        padding-right: 30px;
        font-size: 800;
        color: #f4976c;
    }
    .got-image-upload {
        width: 100%;
        height: 300px;
        background: #b4d5e5;
    }

</style>



        <div class="topnav">
            <a href="begginers-guide.html"> Home </a>   

        </div>
        <div class="home">
            <div class="parallax">
            <h1> War Thunder Social </h1>
        </div>
        </div>
        <div class="got-image-upload">
            <form action="upload.php" method="post" enctype="multipart/form-data">
                Select image to upload:
                <input type="file" name="fileToUpload" id="fileToUpload">
                <input type="submit" value="Upload Image" name="submit">
              </form>
              
        </div>
    </body>




</html>

But I get this error

'File is an image - image/png.Sorry, there was an error uploading your file'

But I don't understand why I'm receiving that error. I have a pictures/ folder, but nothing happens with it, there are no images there. What am I doing wrong with the code? How can I possibly fix it?

Thanks for any suggestion.

Thanks

CodeMonkey
  • 3,271
  • 25
  • 50

2 Answers2

1

The issue is your variable $target_dir is pointing at "~/pictures/", which is unix talk for "/home/user_name/pictures/".

If instead you change it to point at somewhere writeable by apache/PHP:

$target_dir = __DIR__ . '/';

Your form /should/ work (assuming Apache can write to the location serving up the code!).

This is partly to do with Apache configuration, as I would hazard a guess that your home directory is not writeable / known to the server configuration (which is good, it's considered bad practice to allow apache/php to write files in a home directory).

If you'd like to read more, this is the area you'll want to look at:

mikey
  • 1,068
  • 10
  • 13
1

Hi you can use phpUpload class

$pUp = new phpUpload($_FILES['file']);

//$pUp->maxSize('1024');

//$pUp->allowedType(["image/jpeg", "image/png"]);

//$pUp->newName("New_name");

$pUp->run('destination/folder', true);
FF MM
  • 138
  • 9