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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.