Old 02-13-2006   #1 (permalink)
Registered User
 
Join Date: Apr 2005
Location: antwerp, belgium
Posts: 51

lots of help needed

There has always been one thing that didn't work for me and that's uploading files with php. But it is essential for a client so I was wonderding if someone could help me with it. The upload has to be doen in this file.

PHP Code:
<?php
session_start
();
if (!
$_SESSION['admin'])
{
    
header("Location:../index.php");
} else {
include(
"../../connect.php");

$id=$_SESSION["rank"];
$step $_POST[step];

if (
$id <= 3){
if (
$step == ""$step=1;
?>
<link href="../../main.css" rel="stylesheet" type="text/css">
<?php
switch ($step){
    case 
1:
    
?>
        <form action="add.php" method="post" enctype="multipart/form-data">
            <table width="95%" border="0" cellspacing="0" align="center">
              <tr>
                <td>Artist:</td>
                <td><select name="artist">
                  <?php
                      $sql
="SELECT artist_id, artist_name FROM sean_grind_artist ORDER BY artist_name";
                      
$res_sql=mysql_query($sql);
                    while (
$result=mysql_fetch_array($res_sql)){
                          echo 
"<option value=".$result['artist_id'].">".$result['artist_name']."</option>";
                      }
                  
?>
                </select></td>
              </tr>
              <tr>
                <td>Name:</td>
                <td><input type="text" name="name"></td>
              </tr>
              <tr>
                <td>Label:</td>
                <td><input type="text" name="label"></td>
              </tr>
              <tr>
                  <td>Image:</td>
                <td><input type="file" name="file"></td>
              </tr>
              <tr>
                <td>year:</td>
                <td><input name="year" type="text" size="4" maxlength="4"></td>
              </tr>
              <tr>
                <td colspan="2"><input type="hidden" name="step" value="2">
                <input type="submit" name="Submit" value="Continue"></td>
              </tr>
            </table>

        </form>
    <?php
        
break;
    case 
2:
        
$name=$_POST[name];
        
$artist=$_POST[artist];
        
$label=$_POST[label];
        
$year=$_POST[year];
        
$sw_ok=1;
        
$image=$_POST[file];
        
        if (
$image == ""$image="no.jpg";
        
        
$sql="SELECT album_artist, album_name, album_year FROM sean_grind_album";
        
$res_sql=mysql_query($sql);
        while (
$result=mysql_fetch_array($res_sql)){
            if (
$artist == $result['album_artist'] && $name == $result['album_name'] && $year == $result['album_year'])
                
$sw_ok=0;
        }
        
        if (
$sw_ok == 1) {
            
$SQL_update "INSERT INTO sean_grind_album (album_artist, album_name, album_image, album_label, album_year) VALUES ('$artist','$name','$image','$label','$year')";
            
$bool mysql_query($SQL_update);
            if(
$bool == 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('De gegevens zijn aangepast.')</SCRIPT>";
            if(
$bool <> 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('Er is een fout opgetreden bij het bewerken van de gegevens.')</SCRIPT>";
        } else {
            echo 
"This cd has already been submitted";
        }
}
} else {
    echo 
"you don\'t have the permision to view this page";
}
}
?>
file exists out of 2 parts step 1, which is a form and step 2 which ads the data from the form into the database. there is a filefieldin the form named file. Anybody know how to make it work, and upload to a specific folder?
__________________
sneeuwbal is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 02-17-2006   #2 (permalink)
"Enter Random Text Here"
 
sancho's Avatar
 
Join Date: Jun 2005
Location: Handsacre, UK
Posts: 37

Send a message via MSN to sancho Send a message via Skype™ to sancho
This should do what you want

PHP Code:
<?php
session_start
();
if (!
$_SESSION['admin'])
{
    
header("Location:../index.php");
} else {
include(
"../../connect.php");

$id=$_SESSION["rank"];
$step $_POST[step];

if (
$id <= 3){
if (
$step == ""$step=1;
?>
<link href="../../main.css" rel="stylesheet" type="text/css">
<?php
switch ($step){
    case 
1:
    
?>
        <form action="add.php" method="post" enctype="multipart/form-data">
            <table width="95%" border="0" cellspacing="0" align="center">
              <tr>
                <td>Artist:</td>
                <td><select name="artist">
                  <?php
                      $sql
="SELECT artist_id, artist_name FROM sean_grind_artist ORDER BY artist_name";
                      
$res_sql=mysql_query($sql);
                    while (
$result=mysql_fetch_array($res_sql)){
                          echo 
"<option value=".$result['artist_id'].">".$result['artist_name']."</option>";
                      }
                  
?>
                </select></td>
              </tr>
              <tr>
                <td>Name:</td>
                <td><input type="text" name="name"></td>
              </tr>
              <tr>
                <td>Label:</td>
                <td><input type="text" name="label"></td>
              </tr>
              <tr>
                  <td>Image:</td>
                <td><input type="file" name="file"></td>
              </tr>
              <tr>
                <td>year:</td>
                <td><input name="year" type="text" size="4" maxlength="4"></td>
              </tr>
              <tr>
                <td colspan="2"><input type="hidden" name="step" value="2">
                <input type="submit" name="Submit" value="Continue"></td>
              </tr>
            </table>

        </form>
    <?php
        
break;
    case 
2:
        
$name=$_POST[name];
        
$artist=$_POST[artist];
        
$label=$_POST[label];
        
$year=$_POST[year];
        
$sw_ok=1;
        
//$image=$_POST[file];
        
        // $userfile is where file went on webserver
        
$userfile $HTTP_POST_FILES['file']['tmp_name'];
        
// $userfile_name is original file name
        
$userfile_name $HTTP_POST_FILES['file']['name'];
        
// $userfile_size is size in bytes
        
$userfile_size $HTTP_POST_FILES['file']['size'];
        
// $userfile_type is mime type e.g. image/gif
        
$userfile_type $HTTP_POST_FILES['file']['type'];
        
// $userfile_error is any error encountered
        
$userfile_error $HTTP_POST_FILES['file']['error']; 

        
// userfile_error was introduced at PHP 4.2.0
        // use this code with newer versions 

        
if ($userfile_error 0
        {
            echo 
'Problem: ';
            switch (
$userfile_error)
            { 
                case 
1:
                    echo 
'File exceeded upload_max_filesize';
                break;
                case 
2:
                    echo 
'File exceeded max_file_size';
                break;
                case 
3:
                    echo 
'File only partially uploaded';
                break;
                case 
4:
                    echo 
'No file uploaded';
                break;
            }
            exit;
        } 
        if (
$userfile_name == "")
        {
             
$userfile_name="no.jpg";
        }
        else
        {
            
// put the file where we'd like it
            
$upfile "/pathtosave/".$userfile_name;
            
// is_uploaded_file and move_uploaded_file
            
if (is_uploaded_file($userfile))
            {
                if (!
move_uploaded_file($userfile$upfile))
                {
                    echo 
'Problem: Could not move file to destination directory';
                    exit;
                }
            }
            else 
            {
                echo 
'Problem: Possible file upload attack. Filename: '.$userfile_name;
                exit;
            }
            echo 
'File uploaded successfully';
        }
            
        
        
        
$sql="SELECT album_artist, album_name, album_year FROM sean_grind_album";
        
$res_sql=mysql_query($sql);
        while (
$result=mysql_fetch_array($res_sql)){
            if (
$artist == $result['album_artist'] && $name == $result['album_name'] && $year == $result['album_year'])
                
$sw_ok=0;
        }
        
        if (
$sw_ok == 1) {
            
$SQL_update "INSERT INTO sean_grind_album (album_artist, album_name, album_image, album_label, album_year) VALUES ('$artist','$name','$image','$label','$year')";
            
$bool mysql_query($SQL_update);
            if(
$bool == 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('De gegevens zijn aangepast.')</SCRIPT>";
            if(
$bool <> 1) echo "<SCRIPT LANGUAGE=JavaScript>window.alert('Er is een fout opgetreden bij het bewerken van de gegevens.')</SCRIPT>";
        } else {
            echo 
"This cd has already been submitted";
        }
}
} else {
    echo 
"you don\'t have the permision to view this page";
}
}
?>
I've added this code to the above
PHP Code:
//$image=$_POST[file];
        
        // $userfile is where file went on webserver
        
$userfile $HTTP_POST_FILES['file']['tmp_name'];
        
// $userfile_name is original file name
        
$userfile_name $HTTP_POST_FILES['file']['name'];
        
// $userfile_size is size in bytes
        
$userfile_size $HTTP_POST_FILES['file']['size'];
        
// $userfile_type is mime type e.g. image/gif
        
$userfile_type $HTTP_POST_FILES['file']['type'];
        
// $userfile_error is any error encountered
        
$userfile_error $HTTP_POST_FILES['file']['error']; 

        
// userfile_error was introduced at PHP 4.2.0
        // use this code with newer versions 

        
if ($userfile_error 0
        {
            echo 
'Problem: ';
            switch (
$userfile_error)
            { 
                case 
1:
                    echo 
'File exceeded upload_max_filesize';
                break;
                case 
2:
                    echo 
'File exceeded max_file_size';
                break;
                case 
3:
                    echo 
'File only partially uploaded';
                break;
                case 
4:
                    echo 
'No file uploaded';
                break;
            }
            exit;
        } 
        if (
$userfile_name == "")
        {
             
$userfile_name="no.jpg";
        }
        else
        {
            
// put the file where we'd like it
            
$upfile "/pathtosave/".$userfile_name;
            
// is_uploaded_file and move_uploaded_file
            
if (is_uploaded_file($userfile))
            {
                if (!
move_uploaded_file($userfile$upfile))
                {
                    echo 
'Problem: Could not move file to destination directory';
                    exit;
                }
            }
            else 
            {
                echo 
'Problem: Possible file upload attack. Filename: '.$userfile_name;
                exit;
            }
            echo 
'File uploaded successfully';
        } 
You can modify the code to only allow certain file types by using the variable $userfile_type which contains the mime type of the file uploaded
__________________

Portfolio: www.simonmclaughlin.co.uk
Cheap UK hosting: www.qubithosting.com
sancho is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 02-18-2006   #3 (permalink)
Registered User
 
Join Date: Apr 2005
Location: antwerp, belgium
Posts: 51

thx so very much, maybe one more thing you can tell me
the $userfile_name, does it only contain the name of the file or also some more information?
it's for renaming the file before putting it into the daabase
__________________
sneeuwbal is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 02-20-2006   #4 (permalink)
code anyone?
 
scrowler's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Posts: 590

Send a message via MSN to scrowler Send a message via Skype™ to scrowler
$userfile_name would contain only the name of the submitted file, but you can access other information about the file by using the $_FILES superglobal:

PHP Code:
print_r$_FILES["name"] ); 
will output all the data you can use from it. once the file is uploaded you can run regular php functions on the file to obtain more information.
__________________
BioRUST Tutorials - the birthplace
scrowler is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Closed Thread

Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Little asp help needed with simple quiz script developed by Scrowler greer HTML / PHP / ASP / JS 1 01-18-2006 10:55 PM
Flash Designer Needed! Renegade The Marketplace 0 07-02-2004 11:53 AM
been awhile and i needed a new site Milkdrop Showrooms & Works In Progress 11 12-27-2003 01:37 PM


All times are GMT +1. The time now is 08:40 AM.
Content Relevant URLs by vBSEO 3.2.0

Design & Content © BioRUST 2007 :: PRIVACY STATEMENT :: LEGAL INFORMATION :: ADVERTISING MEDIA KIT