PHP Form with File Upload - php Form

Author Topic: PHP Form with File Upload  (Read 12341 times)

Offline cilipe

  • VIP
  • ****
  • Posts: 14
  • Karma: +1/-0
    • View Profile
Submit form data to database
« Reply #15 on: January 04, 2011, 01:01:17 AM »
1. MySQL Query
Code: [Select]
CREATE TABLE `files` (
  `id` int(11) NOT NULL auto_increment,
  `subject` text collate utf8_unicode_ci,
  `firstname` text collate utf8_unicode_ci,
  `lastname` text collate utf8_unicode_ci,
  `city` text collate utf8_unicode_ci,
  `state` text collate utf8_unicode_ci,
  `zip` text collate utf8_unicode_ci,
  `address` text collate utf8_unicode_ci,
  `fax` text collate utf8_unicode_ci,
  `phone` text collate utf8_unicode_ci,
  `website` text collate utf8_unicode_ci,
  `emaile` text collate utf8_unicode_ci,
  `comments` text collate utf8_unicode_ci,
  `newfile` text collate utf8_unicode_ci,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

2. Database Connection (config.php)
Code: [Select]
<?php 
$dbhost 
'localhost';
$dbusername 'root';
$dbpassword '';
$dbname '';
$link=mysql_connect($dbhost$dbusername$dbpassword);
if(!
$link) :
   die(
'Could not connect: ' mysql_error());
endif;
$db=mysql_select_db($dbname$link);
if(!
$db) :
   die (
'Cant connect to database : ' mysql_error());
endif;
?>


3. ccform.php

after
Code: [Select]
mail($emaile, $subject, $bodys, $headers);

Code: [Select]
include('config.php');
if (get_magic_quotes_gpc()) {
$subject = addslashes($subject);
$firstname = addslashes($firstname);
$lastname = addslashes($lastname);
$city = addslashes($city);
$state = addslashes($state);
$zip = addslashes($zip);
$address = addslashes($address);
$fax = addslashes($fax);
$phone = addslashes($phone);
$website = addslashes($website);
$emaile = addslashes($emaile);
$comments = addslashes($comments);
$newfile = addslashes($new_file);
}
$subject = mysql_real_escape_string($subject);
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$city = mysql_real_escape_string($city);
$state = mysql_real_escape_string($state);
$zip = mysql_real_escape_string($zip);
$address = mysql_real_escape_string($address);
$fax = mysql_real_escape_string($fax);
$phone = mysql_real_escape_string($phone);
$website = mysql_real_escape_string($website);
$emaile = mysql_real_escape_string($emaile);
$comments = mysql_real_escape_string($comments);
$newfile = mysql_real_escape_string($new_file);
$result = mysql_query("INSERT INTO files
(subject,firstname,lastname,city,state,zip,address,fax,phone,website,emaile,comments,newfile)
VALUES
('$subject','$firstname','$lastname','$city','$state','$zip','$address','$fax','$phone','$website','$emaile','$comments','$new_file')")
or die(mysql_error());

 :)

Offline technoj

  • Supporter
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: Submit form data to database
« Reply #16 on: January 04, 2011, 06:40:26 AM »
Hello cilipe

does the mail ($,.........)
and the iclude statement below all fall in the same file ccform.php?


Secondly what code do i use as an admin to view submited data from a webpage instead of going all the way to the cpanel to view?

for instance
login.php (for the admin to log in),
view.php(for the admin to view),
search.php(for he admin to search for a particular user)
listall.php(for the admin to list all data posted on the site)

I will be most grateful if i can get help with this

thanks

Offline ElRanska

  • Supporter
  • **
  • Posts: 4
  • Karma: +2/-0
    • View Profile
PHP Form with "multi-Files" Upload
« Reply #17 on: January 14, 2011, 04:57:21 AM »
Hi,

Thank again for this code which work very well.
And again, I am trying to change without any success.

My idea, it's to propose a multi-files upload to the form. It's for send images.
I already change the extension, it was the most easy part.
But, about the part for a multi upload, it didn't work. I got the message "Unknown extension".

The changes I did are on this level
Code: [Select]
$current_file=$_FILES['file']['name'];
$extension = substr(strrchr($current_file, '.'), 1);
if (($extension!= "jpg") && ($extension != "png")) 
{
die('Unknown extension');
}
$time = date("fYhis");
$new_file = $time . "." . $extension;
$destination="uploads/".$new_file;
$action = copy($_FILES['file']['tmp_name'], $destination);
if (!$action)
{
die('File copy failed');
}else{
//echo "File copy successful";
}

that I replace by
Code: [Select]
$current_file=$_FILES['file1']['name'];
$extension = substr(strrchr($current_file1, '.'), 1);
if (($extension!= "jpg") && ($extension != "png")) 
{
die('Unknown extension');
}
$time = date("fYhis");
$new_file1 = $time . "." . $extension;
$destination="uploads/".$new_file1;
$action = copy($_FILES['file1']['tmp_name'], $destination);
if (!$action)
{
die('File copy failed');
}else{
//echo "File copy successful";
}

$current_file=$_FILES['file2']['name'];
$extension = substr(strrchr($current_file2, '.'), 1);
if (($extension!= "jpg") && ($extension != "png")) 
{
die('Unknown extension');
}
$time = date("fYhis");
$new_file2 = $time . "." . $extension;
$destination="uploads/".$new_file2;
$action = copy($_FILES['file2']['tmp_name'], $destination);
if (!$action)
{
die('File copy failed');
}else{
//echo "File copy successful";
}

$current_file=$_FILES['file3']['name'];
$extension = substr(strrchr($current_file3, '.'), 1);
if (($extension!= "jpg") && ($extension != "png")) 
{
die('Unknown extension');
}
$time = date("fYhis");
$new_file3 = $time . "." . $extension;
$destination="uploads/".$new_file3;
$action = copy($_FILES['file3']['tmp_name'], $destination);
if (!$action)
{
die('File copy failed');
}else{
//echo "File copy successful";
}

And, on the form I added
Code: [Select]
<tr>
  <td valign="top">Image 1 (*jpg *png)</td>
  <td height="24"><input type="file" name="file1" ></td>
</tr>
<tr>
  <td valign="top">Image 2 (*jpg *png)</td>
  <td height="24"><input type="file" name="file2" ></td>
</tr>
<tr>
  <td valign="top">Image 3 (*jpg *png)</td>
  <td height="24"><input type="file" name="file3" ></td>
</tr>

in place of
Code: [Select]
<tr>
  <td valign="top">Image (*jpg *png)</td>
  <td height="24"><input type="file" name="file" ></td>
</tr>

What wrong?
Thank for your answer.

Elie

Offline cilipe

  • VIP
  • ****
  • Posts: 14
  • Karma: +1/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #18 on: January 14, 2011, 05:25:09 AM »
Maybe you can try to replace the whole part:

Code: [Select]
if (($extension!= "jpg") && ($extension != "jpeg") && ($extension != "png")) ???


Offline cilipe

  • VIP
  • ****
  • Posts: 14
  • Karma: +1/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #19 on: January 14, 2011, 05:31:20 AM »
Quote
http://phpform.net/board/index.php/topic,341.0.html

Offline ElRanska

  • Supporter
  • **
  • Posts: 4
  • Karma: +2/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #20 on: January 14, 2011, 06:53:29 AM »
Thank for your answer but the problem is not the format.
I didn't see the other code but I change this one to do exactly same. And, it was working for only one image.
In fact, in end, we can propose all kind of format because it's just the extension to change on the script.

The thing I try to do is to add the possibility to upload at least 2 or 3 images at the same time.

Thank anyway.
Elie

Offline cilipe

  • VIP
  • ****
  • Posts: 14
  • Karma: +1/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #21 on: January 14, 2011, 08:34:02 AM »
Find this code:

Code: [Select]
$current_file=$_FILES['file']['name'];
$extension = substr(strrchr($current_file, '.'), 1);
if (($extension!= "txt") && ($extension != "doc")) 
{
die('Unknown extension');
}
$time = date("fYhis");
$new_file = $time . "." . $extension;
$destination="uploads/".$new_file;
$action = copy($_FILES['file']['tmp_name'], $destination);
if (!$action)
{
die('File copy failed');
}else{
//echo "File copy successful";
}
echo"<br>";

replace with this code:

Code: [Select]
$current_file1=$_FILES['file1']['name'];
$current_file2=$_FILES['file2']['name'];
$extension1 = substr(strrchr($current_file1, '.'), 1);
$extension2 = substr(strrchr($current_file2, '.'), 1);
if (($extension1!= "txt") && ($extension1 != "doc") && ($extension2!= "txt") && ($extension2 != "doc")) 
{
die('Unknown extension');
}
$time1 = date("fYhis1");
$time2 = date("fYhis2");
$new_file1 = $time1 . "." . $extension1;
$new_file2 = $time2 . "." . $extension2;
$destination1="uploads/".$new_file1;
$destination2="uploads/".$new_file2;
$action1 = copy($_FILES['file1']['tmp_name'], $destination1);
$action2 = copy($_FILES['file2']['tmp_name'], $destination2);
if (!$action1)
{
die('File copy failed');
}else{
//echo "File copy successful";
}
if (!$action2)
{
die('File copy failed');
}else{
//echo "File copy successful";
}

echo"<br>";

find this:

Code: [Select]
<td height="24"><input type="file" name="file" ></td>

replace with this:

Code: [Select]
<td height="24">
<input name="file1" type="file"><br>
<input name="file2" type="file"><br>
</td> :)


Offline technoj

  • Supporter
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #22 on: February 08, 2011, 10:43:57 PM »
hello Cilipe

thanks for the tutorial on how to add the uploaded files to my database
i have a challenge here,
i want to have a view.php file where i can view the all the fields posted by a user on the site.

The codes below you gave me are working and thanks, but if you can give me the codes for a view.php to view this data posted by a user i will appreciate.

1. MySQL Query
Code: [Select]
CREATE TABLE `files` (
  `id` int(11) NOT NULL auto_increment,
  `subject` text collate utf8_unicode_ci,
  `firstname` text collate utf8_unicode_ci,
  `lastname` text collate utf8_unicode_ci,
  `city` text collate utf8_unicode_ci,
  `state` text collate utf8_unicode_ci,
  `zip` text collate utf8_unicode_ci,
  `address` text collate utf8_unicode_ci,
  `fax` text collate utf8_unicode_ci,
  `phone` text collate utf8_unicode_ci,
  `website` text collate utf8_unicode_ci,
  `emaile` text collate utf8_unicode_ci,
  `comments` text collate utf8_unicode_ci,
  `newfile` text collate utf8_unicode_ci,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

2. Database Connection (config.php)
Code: [Select]
<?php 
$dbhost 
'localhost';
$dbusername 'root';
$dbpassword '';
$dbname '';
$link=mysql_connect($dbhost$dbusername$dbpassword);
if(!
$link) :
   die(
'Could not connect: ' mysql_error());
endif;
$db=mysql_select_db($dbname$link);
if(!
$db) :
   die (
'Cant connect to database : ' mysql_error());
endif;
?>


3. ccform.php

after
Code: [Select]
mail($emaile, $subject, $bodys, $headers);

Code: [Select]
include('config.php');
if (get_magic_quotes_gpc()) {
$subject = addslashes($subject);
$firstname = addslashes($firstname);
$lastname = addslashes($lastname);
$city = addslashes($city);
$state = addslashes($state);
$zip = addslashes($zip);
$address = addslashes($address);
$fax = addslashes($fax);
$phone = addslashes($phone);
$website = addslashes($website);
$emaile = addslashes($emaile);
$comments = addslashes($comments);
$newfile = addslashes($new_file);
}
$subject = mysql_real_escape_string($subject);
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$city = mysql_real_escape_string($city);
$state = mysql_real_escape_string($state);
$zip = mysql_real_escape_string($zip);
$address = mysql_real_escape_string($address);
$fax = mysql_real_escape_string($fax);
$phone = mysql_real_escape_string($phone);
$website = mysql_real_escape_string($website);
$emaile = mysql_real_escape_string($emaile);
$comments = mysql_real_escape_string($comments);
$newfile = mysql_real_escape_string($new_file);
$result = mysql_query("INSERT INTO files
(subject,firstname,lastname,city,state,zip,address,fax,phone,website,emaile,comments,newfile)
VALUES
('$subject','$firstname','$lastname','$city','$state','$zip','$address','$fax','$phone','$website','$emaile','$comments','$new_file')")
or die(mysql_error());

 :)

Offline ciko

  • Contributor
  • *****
  • Posts: 33
  • Karma: +3/-0
  • Gender: Male
    • View Profile
    • phpenter.net
Re: PHP Form with File Upload
« Reply #23 on: February 09, 2011, 06:33:44 AM »
Code: [Select]
<?php
include('config.php');

$tmp mysql_query("SELECT * FROM files ORDER BY id DESC"); 
while (
$row mysql_fetch_assoc($tmp))
{
echo 
"Id: " $row['id'] . "<br />";
echo 
"Subject: " $row['subject'] . "<br />";
echo 
"FirstName: " $row['firstname'] . "<br />";
echo 
"LastName: " $row['lastname'] . "<br />";
echo 
"City: " $row['city'] . "<br />";
echo 
"State: " $row['state'] . "<br />";
echo 
"Zip: " $row['zip'] . "<br />";
/////echo etc...
echo "File: <a href=uploads/" $row['newfile'] . ">File</a><br /><br />";


}
?>
;) ;) ;)

Offline technoj

  • Supporter
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #24 on: February 09, 2011, 06:57:27 PM »
hi
thanks

created a getfile.php and put the codes, all i am getting is the link to the directory(uploads) where the file is and not the file itself, please help

Offline ciko

  • Contributor
  • *****
  • Posts: 33
  • Karma: +3/-0
  • Gender: Male
    • View Profile
    • phpenter.net
Re: PHP Form with File Upload
« Reply #25 on: February 10, 2011, 04:39:44 PM »
Can you try with this file and take me back the errors.

Offline technoj

  • Supporter
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #26 on: February 13, 2011, 10:46:00 PM »
thanks ciko


I had to get the original form and used the codes you gave me, thank you so much.

i will be glad if you can also help me with this for i am learning.

i want to be able to allow the users to be able to verify the data they fill by viewing what they have filled in before submitting the form(this is to help them resett he form incase they make a mistake.

Thank you for your support

Offline technoj

  • Supporter
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #27 on: February 21, 2011, 06:48:23 PM »
Hello People

i havebeen racking my brains for a week now but cant seem to solve this, can anyone please give me a code i can use to like a confirmation page where a user sees the data he has filled before actually sending it?

below are the tables and the ccform as an attachement sent by ciko

pleas e i need this help

thanks
tj

Offline ciko

  • Contributor
  • *****
  • Posts: 33
  • Karma: +3/-0
  • Gender: Male
    • View Profile
    • phpenter.net
Re: PHP Form with File Upload
« Reply #28 on: February 22, 2011, 04:24:16 PM »
Try with this two files  :)

Offline technoj

  • Supporter
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PHP Form with File Upload
« Reply #29 on: February 25, 2011, 07:38:59 AM »
 :)CIKO

thank you very much..it worked like magic..do you have any ideal where i can learn php/database and get a certification for it?

thank you
you are the best!