php - Form Upload Error After Changing Server
I had a working upload form and then recently bought a dedicated hosting plan (GoDaddy - yes, evil I know) for the site that I had been working on on a pre-existing plan. However, although the file structure is exactly the same with full write permissions, the upload no longer works, but the database is updated, despite the DB only being updated if an image is present. I don't understand why the file is no longer being uploaded:
if ($state == "post") {
$title = $_POST['title'];
$body = $_POST['body'];
if(! empty($_FILES['file']['name'][file])) {
$allowed_extensions = array("jpeg", "jpg", "png");
$upload_extension = end(explode(".", $_FILES['file']['name'][file]));
if (in_array($upload_extension, $allowed_extensions)) {
$loop = true;
$im = null;
while ($loop) {
$rand_count = 0;
while ($rand_count != 10) {
$rand = rand(1,9);
$im = $im . $rand;
$rand_count++;
}
$image = $im.".".$upload_extension;
if (!file_exists("images/posts/" . $image)) {
list($width, $height) = getimagesize($_FILES['file']['tmp_name'][file]);
if ($height > $width)
$width_n = 160; $height_n = $height/$width*$width_n;
else
$height_n = 160; $width_n = $width/$height*$height_n;
$tmp_image = imagecreatetruecolor($width_n, $height_n);
if (($upload_extension == "jpeg") || ($upload_extension == "jpg"))
$src = imagecreatefromjpeg($_FILES['file']['tmp_name'][file]);
elseif ($upload_extension == "png")
$src = imagecreatefrompng($_FILES['file']['tmp_name'][file]);
//tmp_upload
imagecopyresampled($tmp_image, $src, 0, 0, 0, 0, $width_n, $height_n, $width, $height);
$path = "images/posts/".$image;
imagejpeg($tmp_image, $path, 100);
$stmt = "INSERT INTO posts (post_title, post_body, post_image) VALUES ('$title', '$body', '$image')";
if (!mysqli_query($con, $stmt))
die('Error: ' . mysqli_error($con));
$loop = false;
header('Location: index.php');
}
}
}
}
}
images/posts
exists on the new server.
Answer
Solution:
Your using the
$_FILES
in a completely wrong manner. In every instance which you use it you need to remove the trailing[file]
ie
becomes