Jump to content

PHP uploader Help


Jragon

Recommended Posts

Hey guys

 

I've made a php uploader,

 <form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="hidden" name="sent"/>
<input type="submit" value="Upload" />

<?php

/**
* @author Jragon
* @copyright 2010
*/
function stringrand()
{
    for ($i = 0; $i < 6; $i++) {
        switch (rand(1, 3)) {
            case 1:
                $rand .= chr(rand(48, 57));
                break; //0-9
            case 2:
                $rand .= chr(rand(65, 90));
                break; //A-Z
            case 3:
                $rand .= chr(rand(97, 122));
                break; //a-z
        }
    }
    return $rand;
}
if (isset($_POST['sent'])) {
    switch ($_FILES['uploaded']['name']) {
        case 'image/jpeg':
            $type = 'image';
            $ext = 'jpg';
            break;
        case 'image/gif':
            $type = 'image';
            $ext = 'gif';
            break;
        case 'image/png':
            $type = 'image';
            $ext = 'png';
            break;
        case 'video/mp4':
            $type = 'video';
            $type = 'mp4';
            break;
        case 'application/x-shockwave-flash':
            $type = 'video';
            $ext = 'flv';
            break;
        case 'audio/mp3':
            $type = 'audio';
            $ext = 'mp3';
            break;
        default:
            $type = '';
            break;
    }
    if ($type == 'video') {
        $name = basename($_FILES['uploaded']['name']);
        $target = "/upload/$type/";
        $target = $target . stringradnd() . ".$ext";
        $worked = false;
        if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
            echo "The file " . $name . " has been uploaded";
            $worked = true;
        } else {
            echo "Sorry, there was a problem uploading your file.";
        }
    } elseif ($type == 'audio') {
        $name = basename($_FILES['uploaded']['name']);
        $target = "/upload/$type/";
        $target = $target . stringradnd() . ".$ext";
        $worked = false;
        if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
            echo "The file " . $name . " has been uploaded";
            $worked = true;
            $target = $target . "&plugins=revolt-1";
        } else {
            echo "Sorry, there was a problem uploading your file.";
        }
    } elseif ($type == 'image') {
        $name = basename($_FILES['uploaded']['name']);
        $target = "/upload/$type/";
        $target = $target . stringradnd() . ".$ext";
        $worked = false;
        if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
            echo "The file " . $name . " has been uploaded";
            $worked = true;
        } else {
            echo "Sorry, there was a problem uploading your file.";
        }
    } elseif ($type == '') {
        $worked = false;
        echo "Sorry the only extensions you can use are:
        jpeg
        jpg
        gif
        png
        mp3
        mp4
        flv";
    }
    if ($worked == true) {
        echo <<< ENDHTML
<script type='text/javascript' src='/flvplayer/swfobject.js'></script>

<div id='mediaspace'>This text will be replaced</div>

<script type='text/javascript'>
  var so = new SWFObject('/flvplayer/player.swf','mpl','470','320','9');
  so.addParam('allowfullscreen','true');
  so.addParam('allowscriptaccess','always');
  so.addParam('wmode','opaque');
  so.addVariable('file','/upload/{$name}');
  so.write('mediaspace');
</script>
ENDHTML;
    }
}

?>

 

But its not working, it dosent upload the files

 

Please help

 

Jragon

Link to comment
https://forums.phpfreaks.com/topic/215032-php-uploader-help/
Share on other sites

it appears you are attempting to upload to /uploads, which would be off the server root (/), and which is almost certainly not allowed. If /uploads is actually off the web server root, you probably want this or something similar instead:

 

$_SERVER['DOCUMENT_ROOT']."/uploads";

 

and check permissions on the upload directory, wherever you put it.

Link to comment
https://forums.phpfreaks.com/topic/215032-php-uploader-help/#findComment-1118557
Share on other sites

Thanks but it still dosent work

 

my new code

 <form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="hidden" name="sent" value="true"/>
<input type="submit" value="Upload" />

<?php

/**
* @author Jragon
* @copyright 2010
*/
function stringrand()
{
    for ($i = 0; $i < 6; $i++) {
        switch (rand(1, 3)) {
            case 1:
                $rand .= chr(rand(48, 57));
                break; //0-9
            case 2:
                $rand .= chr(rand(65, 90));
                break; //A-Z
            case 3:
                $rand .= chr(rand(97, 122));
                break; //a-z
        }
    }
    return $rand;
}
if (isset($_POST['sent'])) {
    echo "step1 <br />";
    switch ($_FILES['file']['type']) {
        case 'image/jpeg':
            $type = 'image';
            $ext = 'jpg';
            break;
        case 'image/gif':
            $type = 'image';
            $ext = 'gif';
            break;
        case 'image/png':
            $type = 'image';
            $ext = 'png';
            break;
        case 'video/mp4':
            $type = 'video';
            $type = 'mp4';
            break;
        case 'application/x-shockwave-flash':
            $type = 'video';
            $ext = 'flv';
            break;
        case 'audio/mp3':
            $type = 'audio';
            $ext = 'mp3';
            break;
        default:
            $type = '';
            break;
    }
    if ($type == 'video') {
        echo "step2 video <br />";
        $name = basename($_FILES['uploaded']['name']);
        $target = $_SERVER['DOCUMENT_ROOT'] . "/projects/uploader/upload/$type/";
        $target = $target . stringradnd() . ".$ext";
        $worked = false;
        if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
            echo "The file " . $name . " has been uploaded";
            $worked = true;
        } else {
            echo "Sorry, there was a problem uploading your file.";
        }
    } elseif ($type == 'audio') {
        echo "step2 audio <br />";
        $name = basename($_FILES['uploaded']['name']);
        $target = $_SERVER['DOCUMENT_ROOT'] . "/projects/uploader/upload/$type/";
        $target = $target . stringradnd() . ".$ext";
        $worked = false;
        if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
            echo "The file " . $name . " has been uploaded";
            $worked = true;
            $target = $target . "&plugins=revolt-1";
        } else {
            echo "Sorry, there was a problem uploading your file.";
        }
    } elseif ($type == 'image') {
        echo "step2 images <br />";
        $name = basename($_FILES['uploaded']['name']);
        $target = $_SERVER['DOCUMENT_ROOT'] . "/projects/uploader/upload/$type/";
        $target = $target . stringradnd() . ".$ext";
        $worked = false;
        if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
            echo "The file " . $name . " has been uploaded";
            $worked = true;
        } else {
            echo "Sorry, there was a problem uploading your file.";
        }
    } elseif ($type == '') {
        $worked = false;
        echo "Sorry the only extensions you can use are:
        jpeg
        jpg
        gif
        png
        mp3
        mp4
        flv";
    }
    if ($worked == true) {
        $target = $_SERVER['DOCUMENT_ROOT'] . "/projects/uploader";
        echo <<< ENDHTML
Well done it worked
<script type='text/javascript' src='$target/flvplayer/swfobject.js'></script>

<div id='mediaspace'>This text will be replaced</div>

<script type='text/javascript'>
  var so = new SWFObject('/flvplayer/player.swf','mpl','470','320','9');
  so.addParam('allowfullscreen','true');
  so.addParam('allowscriptaccess','always');
  so.addParam('wmode','opaque');
  so.addVariable('file','/upload/{$name}');
  so.write('mediaspace');
</script>
ENDHTML;
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/215032-php-uploader-help/#findComment-1119183
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.