file upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

file upload

Post by me666 »

Hi guys, i'm working on a site that has an online application, with the application i am adding an upload feature so they can upload and documents that we will need to see. After looking online i have found a script to do this, but i need to edit it slightly, i can edit smaller parts no problem, but i'm a little confused with other parts.
here are the 3 files that i have so far

Code: Select all

//upload.php
<?php
session_start();
$_SESSION['appid']="leo123";
?>
<html>
<HEAD>
<title>Choose Number of Files to Upload</title>
</head>
<body>
<form name="form1" method="post" action="uploadForm.php">
  <p>How many files would you like to upload? (Max = 9).</p>
  <p>
    <input name="uploadsNeeded" type="text" id="uploadsNeeded" maxlength="1" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
</body>
</html>

Code: Select all

//uploadForm.php
<?php
session_start();
?>
<html>
<title>Upload files</title>
</head>
<body>

<form name="form1" enctype="multipart/form-data" method="post" action="uploadFiles.php">
  <p>
  <?
  $uploadsNeeded = $_POST['uploadsNeeded'];
  for($i=0; $i < $uploadsNeeded; $i++){
  ?>
    <input name="uploadFile<? echo $i;?>" type="file" id="uploadFile<? echo $i;?>" />
  </p>
  <? } ?>
  <p><input name="uploadsNeeded" type="hidden" value="<? echo $uploadsNeeded;?>" />
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
</body>
</html>

Code: Select all

//uploadFiles.php
<?
session_start();
$uploadsNeeded = $_POST['uploadsNeeded'];
for($i = 0; $i < $uploadsNeeded; $i++){
$file_name = $_FILES['uploadFile'. $i]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$file_name = $_SESSION[appid]."-"."$file_name";
$copy = copy($_FILES['uploadFile'. $i]['tmp_name'],"uploaded/". $file_name);
 // prompt if successfully copied
 if($copy){
 echo "$file_name | uploaded sucessfully!<br>";
 }else{
 echo "$file_name | could not be uploaded!<br>";
 }
}
?>
I have made a few changes to what i had found online, but i now need to make it so that,
1. you may only upload images(scanned documents) or document files(although anything scanned wont come as a document file would it?)
2.the file name can be changed (have done this partially as you can see with the session) so that the saved file will be $_SESSION[appid]-doc1.blah even though the file uploaded was named scan50.blah, i managed this but lost the extension meaning the saved file was just $_SESSION[appid]-doc1 (if this makes sence to you)
3. i would like there to be an add upload button so that you dont say at begining how many files u will upload, using document.write i imagine, but when i used this in the past i lost the rest of the page :S

anything you can help me with here would be greatfully appreciated.

Thanks :D
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: file upload

Post by TonsOfFun »

For number 1: add an if statement:

Code: Select all

if($_FILE['uploadFile'. $i]['type'] == 'image/jpeg' || $_FILE['uploadFile'. $i]['type'] == 'image/pjpeg') {
//then proceed
image/pjpeg is for ie.
You can add other file extentions to this and add another or (||) to accept that file type.

For number 2: have and if statement

Code: Select all

if($type == 'image/jpeg') { $ext = '.jpg';}
add and elseif for more file extentions.
Then change how you name the file to:

Code: Select all

$file_name = $_FILES['uploadFile'. $i]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
//add the next line
$file_name = $file_name . $ext;

$file_name = $_SESSION[appid]."-"."$file_name";
This will append the extention, which is determined from the if statement, the the file name

I'm not sure about number 3, but check out this blog. It should do the trick.
http://binnyva.blogspot.com/2006/01/dyn ... on-in.html
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: file upload [solved]

Post by me666 »

Excelent, thank yo for that, i had a feeling it would be IF statements, but unsure how to use the type part, i have added theese and added a javascript button to add more fields as explained in the link you left me, works a treat :D
Post Reply