Request help with uploading an image please.

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 115
Joined: Tue Mar 01, 2016 3:07 pm

Tue Mar 29, 2016 10:36 am

Hello (sorry for my third post in a week), I thought I'd already sorted this out.

I've attempted to write some php code which would upload a image to the server. However my image files are been rejected. As I'm attempting to upload a gif file, I'm unsure why I get File is not an image message.

May I ask if my code is correct please?
I've used an echo to check:- $fileToUpload = $_POST["filAdmin_Photo"]; , and it is receiving the file name & path ok.

// Create The Upload File PHP Script
$target_dir = "setup/";
$fileToUpload = $_POST["filAdmin_Photo"];

// echo $fileToUpload; //OK - filename

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); // Something not right here.
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// echo $target_file; // directory



// Check if image file is a actual image or fake image
// if(isset($_POST["submit"])) {
$check = getimagesize($_DIR["target_file"].$_FILES["fileToUpload"]["tmp_name"]);
echo $check;
exit();
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;
}

Thank you.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Tue Mar 29, 2016 12:00 pm

Rather than try to fix the script, go back to the original script and if you need to make changes, do them one thing at a time and test it thoroughly.

Your form variables do not match the variable names that you are using in the script along with a couple of other changes that you have made.

IChaps
New php-forum User
New php-forum User
Posts: 115
Joined: Tue Mar 01, 2016 3:07 pm

Wed Mar 30, 2016 3:23 pm

Hello.

I'm still struggling with uploading an image. I've deleted my file twice, and started again twice, but I'm unable to upload a image file.
At present my code now reads, (however I've inserted an echo while I'm trying to sort this out):-
<?php
$uploadfile = $_POST["filAdmin_Photo"];

echo $uploadfile;

$uploaddir = "uploads/"
$filename = trim($_FILES["uploadfile"] ["name"]);
$filename = substr($filename, - 20);
$filename = ereg_replace(" ", "", $filename);

if((ereg(".jpg", $filename)) || (ereg(".gif", $filename))) {
$uploadfile = $uploaddir . $filename;
if (move_uploaded_files($_FILES['uploadfile']['tmp_name'], $uploadfile)) {
chmod($uploadfile, 0644);
print ("File upload was successful.");
} else {
print ("File upload failed.");
}
} else {
print ("Only images allowed, upload failed.");
}
?>


The server log reports:- mod_fcgid: stderr: PHP Parse error: syntax error, unexpected T_VARIABLE . I have marked the line with the error message in bold.
I would like to request (if possible),

If the above is not correct, If anyone could write the code for me please?
Thank You

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Wed Mar 30, 2016 4:46 pm

Code: Select all

<?php
$uploadfile = $_POST["filAdmin_Photo"];

echo $uploadfile;

$uploaddir = "uploads/" /////////////////////////////////////////This line has the error
$filename = trim($_FILES["uploadfile"] ["name"]);
$filename = substr($filename, - 20);
$filename = ereg_replace(" ", "", $filename);

if((ereg(".jpg", $filename)) || (ereg(".gif", $filename))) {
$uploadfile = $uploaddir . $filename;
if (move_uploaded_files($_FILES['uploadfile']['tmp_name'], $uploadfile)) {
chmod($uploadfile, 0644);
print ("File upload was successful.");
} else {
print ("File upload failed.");
}
} else {
print ("Only images allowed, upload failed.");
}
?>
The error is in the line before the one that you highlighted.
If anyone could write the code for me please?
You have already copied the code from another site, why are you asking someone else to write you a script; I've no problem helping, but you will not learn anything by copying then changing scripts and wondering why it won;t do what you think it should do.

Post Reply