I cant seem to upload images through a form

if (!isset($_POST["photo"])) {
                $target_dir = "ProfilePics/";
                $target_file = $target_dir.strtolower($_SESSION['username']).'.jpg';
                $imageFileType = $ext = pathinfo($_FILES["photo"]["name"], PATHINFO_EXTENSION);
                echo $imageFileType;
                if ($imageFileType == "jpg" or $imageFileType== "jpeg" or $imageFileType== "png") {
                    echo $target_file.'<br>';
                    echo $_FILES["photo"]["name"].'<br>';
                    if (copy($_FILES["photo"]["tmp_name"], $target_file)) {
                        echo "Moved <br>";
                    }
                }
            }

This is my Upload script

<form method="POST" enctype="multipart/form-data">
                                            <input type="file" class="form-control" name="photo" id="photo">
                                            <img src="/ProfilePics/'.strtolower($_SESSION['username']).'.jpg" alt="Circle Image" class="img-circle img-responsive img-raised" rel="tooltip" title="Click to change picture">
                                        </form>

This is my Form

$_FILES[“photo”] is empty I don’t know why, website URL is craftycorpx.rf.gd
I am new to PHP

<form method="POST" enctype="multipart/form-data">
                                            <input type="file" class="form-control" name="photo" id="photo">
                                            <img src="/ProfilePics/'.strtolower($_SESSION['username']).'.jpg" alt="Circle Image" class="img-circle img-responsive img-raised" rel="tooltip" title="Click to change picture">
                                        </form>

Here is the Form

if (!isset($_POST["photo"])) {
                $target_dir = "ProfilePics/";
                $target_file = $target_dir.strtolower($_SESSION['username']).'.jpg';
                $imageFileType = $ext = pathinfo($_FILES["photo"]["name"], PATHINFO_EXTENSION);
                echo $imageFileType;
                if ($imageFileType == "jpg" or $imageFileType== "jpeg" or $imageFileType== "png") {
                    echo $target_file.'<br>';
                    echo $_FILES["photo"]["name"].'<br>';
                    if (copy($_FILES["photo"]["tmp_name"], $target_file)) {
                        echo "Moved <br>";
                    }
                }
            }

Here is the PHP upload script

Are you uploading a valid image according to the code? It should be a .jpg, .jpeg, or a .png extension.

I’m a bit confused by this line:

if (!isset($_POST["photo"])) {

So you want to trigger the form if the value is NOT set? Shouldn’t you trigger the upload script if the file is set?

3 Likes

oh thanks didn’t realise there was an !

Still didn’t work

You mean you change it and it doesn’t work? Has it been validated yet? Plus the target directory should not be ProfilePics/ it should be /ProfilePics/ or ./ProfilePics/ .

This is a relative url and url is different than path. PHP looks for path not url.

It should be __DIR__.'/ProfilePics/'.

2 Likes

Umm . . . how is your form being submitted? I don’t see a submit button.

1 Like

I did everything but it seems like isset($_FILES[“photo”]) returns false

also isset($_POST[“photo”]) also returns nil

 <div class="row">
                <div class="col-xs-6 col-xs-offset-3">
    	           <div class="profile">
                        <div class="avatar">
                        <?php
                            if (isset($_POST["edit"])) {
                                echo '<form method="POST" enctype="multipart/form-data">
                                        <input type="file" class="form-control" name="photo" id="photo">
                                        <img src="/ProfilePics/'.strtolower($_SESSION['username']).'.jpg" alt="Circle Image" class="img-circle img-responsive img-raised" rel="tooltip" title="Click to change picture">
                                    </form>';
                            } else {
                            echo '<img src="/ProfilePics/'.strtolower($_SESSION['username']).'.jpg" alt="Circle Image" class="img-circle img-responsive img-raised">';
                            }
                        ?>
                        </div>
                        <div class="name">
                            <h3 class="title"><?php
                                echo $_SESSION["username"];
                            ?></h3>
							<h6><? echo $conn->query('SELECT `Rank` FROM `Users` WHERE `UserName` = "'.strtoupper($_SESSION['username']).'"')->fetchcolumn(); ?></h6>
                        </div>
                    </div>
	            </div>
                <div class="col-xs-2 follow">
                    <form method="POST">
                   <button type="submit" name="edit" class="btn btn-fab btn-primary" rel="tooltip" title="Edit">
                        <i class="material-icons">edit</i>
                    </button>
                    </form>
                </div>
            </div>


            <div class="description text-center">
                <p><?php
                    if (isset($_POST["edit"])) {
                        echo '<form method="POST" enctype="multipart/form-data">
                                <input type="Text" class="form-control" name="description" Placeholder="Description..." Value="'.$_SESSION["description"].'">
                                <button type="submit" class="btn btn-fab btn-primary" rel="tooltip" title="Set Description">
                        <i class="material-icons">checkmark</i>
                    </button>
                            </form>';
                    } else {
                    echo $_SESSION["description"];}
                ?></p>
            </div>