Why doesn't PHP upload the file?

ok here’s the code:

This is my first time attempting to upload files… with php
[php]

Profile Image  [/php]

The upload script
[php]

<?php session_start(); error_reporting("E_ALL"); $ID = $_POST['HIDE']; $IMGTYPE = pathinfo($_FILES['Avatar']['name'], PATHINFO_EXTENSION); $error = false; $msg = ""; if($_FILES['Avatar']['tmp_name'] > 5000){ $msg .= "File too big
". PHP_EOL; $error = true; } if(!ctype_digit($ID)){ $msg .= "ERROR: ID is not of thou art numerical value" . PHP_EOL; $error = true; } if(file_exists($ID.$IMGTYPE)){ $msg .= "ERROR: ID is not of thou art existance" . PHP_EOL; $error = true; } if($IMGTYPE != "jpg" || $IMGTYPE != "png" || $IMGTYPE != "gif" || $IMGTYPE != "jpeg"){ $error = true; $msg .= "File MUST be jpg, png, jpeg, or gif!
" . PHP_EOL; } if(!$error){ //if no errors then upload file //grab whole $Avatar_DB = $ID.".".$IMGTYPE; // move_uploaded_file($_FILES['Avatar']['tmp_name'], "Avatars/".$ID.".".$IMGTYPE) or die("File not uploaded"); //connect to DB require_once("../Scripts/DB/connect.php"); //create a query to update the avatar $Query = $connect->prepare("UPDATE Users SET Avatar=:avatar"); $Query->bindValue(":avatar", $Avatar_DB); $Query->execute() or die("Image Unsuccessful"); $User = $_SESSION['Logged_in']; header("Location: ./profile.php?user=$User&&id=$ID&&upload=true"); exit(); } else { echo "

.$msg.

"; } ?>

[/php]

I get the file not uploaded script.

For one, your missing the correct enctype

multipart/form-data?

Ok, just googled it. Would the cause the error in upload? :frowning:

Yes, this is the correct form:
[php][/php]

As for an upload script, why write one on your own when W3S already did the work for you?
[php]<?php
$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = 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;
}
// Check file size
if ($_FILES[“fileToUpload”][“size”] > 500000) {
echo “Sorry, your file is too large.”;
$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 “. basename( $_FILES[“fileToUpload”][“name”]). " has been uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
}
?>[/php]

It looks like you are using object-oriented Mysqli; I’m pretty sure that:
[PHP]$Query->bindValue(":avatar", $Avatar_DB);[/PHP]
is incorrect, you need to use bind_param

No, PDO… ;D
bindValue() is only bound as a reference until PDOStatement::execute() is used.

Anyways, the reason I used this one is I want the user avatar stored with an ID. I check to make sure the hidden form value wasn’t injected, by checking that it’s ALL integer values.

Sponsor our Newsletter | Privacy Policy | Terms of Service