Jump to content

Upload Photo, Change File Name, Save file name to DB


Adamhumbug

Recommended Posts

HI All,

I have a form submission that uploads a photo as well as submitting other data.

I would like to change the name of the photo to the id of the person record (created automatically on by the database) then a hyphen, then their first name and lastname. (i am flexible on this).

This file name will also need to be submitted into the person record so the photo and the person can be linked.

I am struggling with this one - but here is the code i have so far.

 

<?php 
include 'includes/dbconn.php';

$target_dir = "img/people/";
$target_file = $target_dir . basename($_FILES["personHeadshot"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if ($_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'];


	$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) 
	                       VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
	                       ");
	$stmt -> bind_param(ssiiissssssi, $fn, $ln, $crole, $playerType, $team, $ad1, $ad2, $city, $post, $tel, $email, $OFA);
	$stmt -> execute();

	// Check if image file is a actual image or fake image
//photo upload
	$check = getimagesize($_FILES["personHeadshot"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
//photo upload

	header("location: ../admin-people-list.php");
}
// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["personHeadshot"]["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["personHeadshot"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["personHeadshot"]["name"]). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}

 

Link to comment
Share on other sites

I worked it out

 

<?php 
include '../includes/dbconn.php';



if ($_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'];
	

	 $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 excately 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) 
	                       VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
	                       ");
	$stmt -> bind_param(ssiiissssssis, $fn, $ln, $crole, $playerType, $team, $ad1, $ad2, $city, $post, $tel, $email, $OFA, $personHeadshot);
	$stmt -> execute();



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


}

 

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.