New Posts  All Forums:Forum Nav:

upload file [PHP]

post #1 of 3
Thread Starter 
upload.html (Click to show)
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
            "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>uMovies</title>
<style type="text/css">
@import url(uMovies.css);
</style>
</head>
<SCRIPT language=JavaScript>
    function verify( form ) {
        file = form.elements[ "uploaded" ];
        if (( file.value != null ) && ( file.value != "" )) {
            return confirm("Upload " + file.value + "?");
        }
        alert( "Please provide a filename." );
        return false;
    }
</SCRIPT>
<body>

<div id="links">
<a href="index.html">Home<span> Access movies, actors and directors.</span></a>
<a href="admin.html">Administrator<span> Administrator access. Password required.</span></a>
<a href="movies.php">Movies<span> Browse all movies.</span></a>
<a href="actors.php">Actors<span> Browse all actors.</span></a>
<a href="directors.php">Directors<span> Browse all directors.</span></a>
</div>

<div id="content">
<h1>uMovies&trade;</h1>
<center>
<h2>Upload to database</h2>

<form enctype="multipart/form-data" action="upload.php" method="post" onsubmit="return verify(this);">
<input type="hidden" name="MAX_FILE_SIZE" value="1000" /> File: <input type="file" name="uploaded" size="30" />
<p><input type="submit" value="Upload" /></p>
</form>


</center>
</div>

</body>
</html>

leads to this :
upload.php (Click to show)
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
            "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>uMovies :: Actors</title>
<style type="text/css">
@import url(uMovies.css);
</style>
</head>
<body>

<div id="links">
<a href="index.html">Home<span> Access movies, actors and directors.</span></a>
<a href="admin.html">Administrator<span> Administrator access. Password required.</span></a>
<a href="movies.php">Movies<span> Browse all movies.</span></a>
<a href="actors.php">Actors<span> Browse all actors.</span></a>
<a href="directors.php">Directors<span> Browse all directors.</span></a>
</div>

<div>
<?php
    $okay = true;
        if ($_FILES[ 'uploaded' ][ 'error' ] > 0) {
                echo "Error: " . $_FILES["file"]["error"] . "<br>";
                switch ($_FILES[ 'uploaded' ][ 'error' ]) {
                        case 1: echo '* File exceeded maximum size allowed by server.<br/>';        break;
                        case 2: echo '* File exceeded maximum size allowed by application.<br/>';         break;
                        case 3: echo '* File could not be fully uploaded.<br/>';                    break;
                        case 4: echo '* File was not uploaded.<br/>';
                }
                $okay = false;
        }
        if ($okay && $_FILES[ 'uploaded' ][ 'type' ] != 'text/plain') {
                echo 'A problem was detected:<br/>';
                echo '* File is not a text file.<br/>';
                $okay = false;
        }
        $filename = ['uploaded'];
        if ($okay) {
                if (is_uploaded_file($_FILES[ 'uploaded' ][ 'tmp_name' ])) {
                        if (!move_uploaded_file($_FILES[ 'uploaded' ][ 'tmp_name' ], $filename)) {
                                echo 'A problem was detected:</br>';
                                echo '* Could not copy file to final destination.<br/>';
                                $okay = false;
                        }
                }
                else {
                        echo 'A problem was detected:<br/>';
                        echo '* File to copy is not an uploaded file.<br/>';
                        $okay = false;
                }
        }
        if($okay){
                echo 'File uploaded successfully.';
                $file = fopen($filename, 'r' );
                $fileContents = nl2br(fread( $file, filesize( $filename )));
        fclose ( $file );
                echo "<hr/> $fileContents <hr/>";
        }
?>
        

</div>
</body>
</html>

For now i just want to show the contents of the file on that php file but all im getting is the code written on the page, i dont get it! I followed how we were taught. Any ideas? (sorry if it's obvious im new!!)

this is what i get!!



thanks for any help biggrin.gif rep to be offered thumb.gif
Edited by protzman - 12/6/12 at 6:39pm
post #2 of 3
You need to use PHP. Here check this out
Code:
<?php

    $mysql_server       = 'localhost';
    $mysql_username     = 'root';
    $mysql_password     = '';
    $mysql_db_name      = 'final_project';
    $mysql_table_name   = 'software_engineering';
    
    mysql_connect($mysql_server, $mysql_username, $mysql_password) or die('Could not connect to database');
    mysql_select_db("$mysql_db_name")or die('cannot find the selected database');
        
    $query = "SELECT * FROM `$mysql_table_name` WHERE username = '$_POST[username]' and password = '$_POST[password]'";
        
    if($query_run = mysql_query($query)) {
        
        $count = mysql_num_rows($query_run);
        
        if($count==1)
                {
                        
                        session_start();
                        $_SESSION['username'] = $_POST[username];
                        
                        echo $_SESSION['username'];
                        
                }
        else
                {
            echo 'your username or passsword are wrong';   
                }
                             
    }
    else
        echo 'query failed';
        
?>


*Note:- Be careful because this code is not protected from sql injection. Since its just a school project then don't worry about it. However I recommend that you give this a read.
http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection
Edited by Fantasy - 12/3/12 at 11:19pm
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
post #3 of 3
Thread Starter 
Thanks for the help i haven't had the chance to implement it yet, because i had to knock out another project. Just wanted to say thanks tho and im gonna look @ it in a min!
+ rep
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming