php problem

This is a discussion on "php problem" within the PHP Forum section. This forum, and the thread "php problem are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 16th, 2006, 17:54
Junior Member
Join Date: Nov 2006
Location: us
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
php problem

Hi,
Is it possible to merge the html form with php code on the one page for file upload and how can I make the uploads for mp3,wav and ppt format on the php code section? If I want to insert a record to this html form and upload php page, is that possible? Any thoughts will be more appreciate!
This is the upload form in the html page 'fileupload.html'
HTML: Select all
<body>
<form action="fileuploads.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="950000" />
<p><strong>File to Upload:</strong>
<input type="file" name="fileupload" /></p>
<p><input type="submit" value="upload!" /></p>
</form>
</body>
</html>
This is the fileuploads.php page
PHP: Select all

 <?php 
$file_dir 
"images"
foreach(
$_FILES as $file_name => $file_array){ 
    echo 
"path: ".$file_array['tmp_name']."<br>\n"
    echo 
"name: ".$file_array['name']."<br>\n"
    echo 
"type: ".$file_array['type']."<br>\n"
    echo 
"size: ".$file_array['size']."<br>\n"
     
    if(
is_uploaded_file($file_array['tmp_name'])){ 
        
move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or die("Couldn't copy"); 
        echo 
"File was uploaded!<br><br>"
        echo 
"<a href='fileupload.html'> More uploads...</a>";     
    } 

?>
Reply With Quote

  #2 (permalink)  
Old Dec 18th, 2006, 16:18
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: php problem

It's perfectly possible to include the php that processes the form in the same file as your html.

You will have to have the html file parsed by the php parser of course. You can do this by changing the file extention from .html to .php but my prefered method is to keep the .html extention and have an entry in your .htaccess file that tells the server to pass any .html file through the php parser.

The 'action' atribute in your in your 'form' element can then just look like action=''. Basically meaning process myself.

You will need to add code into the php so that it can tell the difference betwen when the file is just being displayed and when it needs to process the form data.

You can upload any type of file using the form method. How you detect the type and process it comes down to your php code.
Reply With Quote
  #3 (permalink)  
Old Dec 18th, 2006, 18:51
Junior Member
Join Date: Nov 2006
Location: us
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Re: php problem

I have just copied the form action code from html page and pasted it right before the <?php tag and changed it form action code into '<?php echo $_SERVER['PHP_SELF']; ?>'. The uploadfile.php runs the action itself, it works. But I am not sure how to deal with the insert record button once a file uploaded?How do I do this insert string? Thanks for your help.
Reply With Quote
  #4 (permalink)  
Old Dec 18th, 2006, 21:19
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: php problem

Can you be a bit more explicite about what you are doing?
Reply With Quote
  #5 (permalink)  
Old Dec 19th, 2006, 04:49
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: php problem

PHP: Select all

 <?php
if(is_set($_POST['upload!']))
{
    
$file_dir "images"
    foreach(
$_FILES as $file_name => $file_array){ 
        echo 
"path: ".$file_array['tmp_name']."<br>\n"
        echo 
"name: ".$file_array['name']."<br>\n"
        echo 
"type: ".$file_array['type']."<br>\n"
        echo 
"size: ".$file_array['size']."<br>\n"
     
        if(
is_uploaded_file($file_array['tmp_name'])){ 
            
move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or die("Couldn't copy"); 
            echo 
"File was uploaded!<br><br>"
            echo 
"<a href='fileupload.html'> More uploads...</a>";     
        } 
    }
} else {
?> 
<body>
<form action="fileuploads.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="950000" />
<p><strong>File to Upload:</strong>
<input type="file" name="fileupload" /></p>
<p><input type="submit" value="upload!" /></p>
</form>
</body>
</html>
<?php ?>
Reply With Quote
  #6 (permalink)  
Old Dec 19th, 2006, 05:00
Junior Member
Join Date: Nov 2006
Location: us
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Re: php problem

What I wanted to do was to insert a record along with uploading file to the artwork table in the database. Obviously I have such hard time to figure out what to do on this insert.php page.
The gallery database contains 3 table below:
1.artwork (id,category(int),media(int), photo)
2.category(id,type)
3.media(id,kind)
Quote:
<?php require_once('Connections/gallery_conn.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO artwork (caption, category, media, photo) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['caption'], "text"),
GetSQLValueString($_POST['category'], "int"),
GetSQLValueString($_POST['media'], "int"),
GetSQLValueString($_POST['photo'], "text"));

mysql_select_db($database_gallery_conn, $gallery_conn);
$Result1 = mysql_query($insertSQL, $gallery_conn) or die(mysql_error());

$insertGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_gallery_conn, $gallery_conn);
$query_rst_cat = "SELECT * FROM category";
$rst_cat = mysql_query($query_rst_cat, $gallery_conn) or die(mysql_error());
$row_rst_cat = mysql_fetch_assoc($rst_cat);
$totalRows_rst_cat = mysql_num_rows($rst_cat);

mysql_select_db($database_gallery_conn, $gallery_conn);
$query_rst_media = "SELECT * FROM media";
$rst_media = mysql_query($query_rst_media, $gallery_conn) or die(mysql_error());
$row_rst_media = mysql_fetch_assoc($rst_media);
$totalRows_rst_media = mysql_num_rows($rst_media);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>fileupload</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" name="form1" enctype="multipart/form-data" method="POST">
Caption:
<input name="caption" type="caption" />
<br />
Category:<select name="category" id="category">
<?php
do {
?>
<option value="<?php echo $row_rst_cat['cat_ID']?>"><?php echo $row_rst_cat['type']?></option>
<?php
} while ($row_rst_cat = mysql_fetch_assoc($rst_cat));
$rows = mysql_num_rows($rst_cat);
if($rows > 0) {
mysql_data_seek($rst_cat, 0);
$row_rst_cat = mysql_fetch_assoc($rst_cat);
}
?>
</select><br />
Media:<select name="media" id="media">
<?php
do {
?>
<option value="<?php echo $row_rst_media['media_ID']?>"><?php echo $row_rst_media['kind']?></option>
<?php
} while ($row_rst_media = mysql_fetch_assoc($rst_media));
$rows = mysql_num_rows($rst_media);
if($rows > 0) {
mysql_data_seek($rst_media, 0);
$row_rst_media = mysql_fetch_assoc($rst_media);
}
?>
</select>
<input type="hidden" name="MAX_FILE_SIZE" value="950000" />
<p><strong>File to Upload:</strong>
<input type="file" name="photo" />
</p>
<p>
<input name="submit" type="submit" value="Insert" />
</p>

<input type="hidden" name="MM_insert" value="form1">
</form>
<?php
$file_dir = "images";
foreach($_FILES as $file_name => $file_array){
echo "path: ".$file_array['tmp_name']."<br>\n";
echo "photo: ".$file_array['photo']."<br>\n";
echo "type: ".$file_array['type']."<br>\n";
echo "size: ".$file_array['size']."<br>\n";

if(is_uploaded_file($file_array['tmp_name'])){
move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[photo]") or die("Couldn't copy");
echo "File was uploaded!<br><br>";
echo "<a href='insert.php'> Add more record...</a>";
}
}
?>
</body>
</html>
<?php
mysql_free_result($rst_cat);

mysql_free_result($rst_media);
?>
Reply With Quote
Reply

Tags
problem

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
First image problem and inline list problem konnor5092 Web Page Design 8 Dec 1st, 2007 09:08


All times are GMT. The time now is 06:57.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43