Jump to content

Php file upload not working


Adamhumbug

Recommended Posts

I have a form that includes uploading a headshot.

This is the form handling

f ($_SERVER['REQUEST_METHOD']=='POST'){


	$fn = $_POST['fname'];
	$ln = $_POST['lname'];
	$ad1 = $_POST['ad1'];
	$ad2 = $_POST['ad2'];
	$city = $_POST['city'];
	$post = $_POST['postcode'];
	$tel = $_POST['phone'];
	$email = $_POST['email'];
	$crole = $_POST['comRole'];
	$OFA = $_POST['OFA'];
	$playerType = $_POST['playerType'];
	$team = $_POST['primaryTeam'];
	$dob = $_POST['dateOfBirth'];
	
	if(!empty($_FILES['fileToUpload'])){
		$errors= array();
	 
      $file_name = $_FILES['personHeadshot']['name'];
      $file_size =$_FILES['personHeadshot']['size'];
      $file_tmp =$_FILES['personHeadshot']['tmp_name'];
      $file_type=$_FILES['personHeadshot']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['personHeadshot']['name'])));

      $newFileName = $fn."-".$ln.".".$file_ext;
      
      $extensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152){
         $errors[]='File size must be smaller than 2 MB';
      }
      
      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"img/people/".$newFileName);
         echo "Success";
      }else{
         print_r($errors);
      }

      $personHeadshot = $newFileName;
	}
	 

	$stmt = $conn->prepare("
	                       INSERT IGNORE INTO person (fname, lname, committee_role_id, player_type_id, team_id, ad1, ad2, city, postcode, mobile, email, on_field_auth_id, image, dob) 
	                       VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
	                       ");
	$stmt -> bind_param('ssiiissssssiss', $fn, $ln, $crole, $playerType, $team, $ad1, $ad2, $city, $post, $tel, $email, $OFA, $personHeadshot, $dob);
	$stmt -> execute();



	header("location: admin-people-list.php");


}

I also have this file


<?php
$target_dir = "img/people/";
$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;
}

// 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.";
  }
}
?>

This was working locally but now i have uploaded it i cannot get it to work.

All of the person data gets put into the DB asside from the file name

I appreciate you may need more info to help me which i will provide on request

Link to comment
Share on other sites

18 minutes ago, Adamhumbug said:

This was working locally but now i have uploaded it i cannot get it to work.

"cannot get it to work" means nothing. What do you expect it to do and what does it actually do? Are there any error messages?

On the subject of error messages, you need to check for errors with the file upload before you try to do anything with it. Addressing this in your code now will likely help you identify the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.