How To Upload Images (Need Short PHP Script PLEASE!!)

Discussion in 'PHP' started by Magestry, Nov 18, 2008.

  1. #1
    Hey guys,

    I need a short PHP script that can upload images into my website.

    The script would be in /admin/ but I need the files to go into /dog-pictures/ so can anybody help me make a script that can upload the images into the folder, as simple as that.

    Thanks if so!
     
    Magestry, Nov 18, 2008 IP
  2. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #2
    Well I can make it.

    PM me if interested :)
     
    harrisunderwork, Nov 18, 2008 IP
  3. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #3
    make a new .php page
    write this
    <form action="" method="POST" enctype="multipart/form-data">
    File <input type="file" name="pic">
    <input type="submit" name="sb" id="sb" value="upload">
    </form>
    <?php
    if(isset($_POST['sb'])) {
    $path = "../dog-pictures ";
    $file = $_FILES['pic']['tmp_name'];
    $newpath = "$path/$file";
    move_uploaded_file($_FILES['pic']['tmp_name'], $path);
    }
    ?
     
    crivion, Nov 18, 2008 IP
    Magestry likes this.
  4. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Yeh, thanks, it worked!
    +rep for yuo, thanks m8!
     
    Magestry, Nov 18, 2008 IP
  5. alhelalat

    alhelalat Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    this code is missing to check the file weather it is image or not ..
    what if some one upload php script and hack your site ???
     
    alhelalat, Nov 19, 2008 IP
  6. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #6
    he says he will use in admin area ! why he as admin will want to hack his own site. also it's a quickie script
     
    crivion, Nov 19, 2008 IP
  7. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Yup only admin wants to upload the image .
     
    harrisunderwork, Nov 19, 2008 IP
  8. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Yeh, thanks guys, just one more question, in my admin bit, I also have a bit where I can edit a text file that I can update all the variables iin ti for my dog pictures, but when it is in the text box, it only shows the first line... how can I fix this, here is the code I have:

    
    <?php
    if (isset($_POST['submit'])) {
    
    $myFile = "*my file, private*";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = stripslashes($_POST['sf']);
    fwrite($fh, $stringData);
    fclose($fh);
    
    header('Location: index.php?a=done');
    
    }
    ?>
    <HTML>
    <body>
    
    <form action="" method="post">
    <textarea name="sf" cols="80" rows="10">
    <?php
    $myFile = "*my file, private*";
    $fh = fopen($myFile, 'r');
    $theData = fgets($fh);
    fclose($fh);
    echo $theData;
    ?></textarea>
    <br />
    <input type="submit" name="submit" value="Edit" />
    </form>
    
    <?php
    if ($_GET['a'] == 'done') {
    echo 'The file was saved and now it says:<br /><br />';
    
    $myFile = "*my file, private*";
    $fh = fopen($myFile, 'r');
    $theData = fgets($fh);
    fclose($fh);
    echo $theData;
    
    }
    ?>
    </body>
    
    Code (markup):
    The file I want to edit is a text file and it is like this, but not the actual thing lol,

    
    <?php
    	if ($name == "name1") $nameR = "Name One";
    	else $nameR = "Not Found";
    ?>
    
    Code (markup):
    But all it shows in the text box is <?php and I can't seem to fix it, any help?

    Thanks
     
    Magestry, Nov 19, 2008 IP
  9. xcrox

    xcrox Banned

    Messages:
    232
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    <form action="" method="POST" enctype="multipart/form-data">
    File <input type="file" name="pic">
    <input type="submit" name="sb" id="sb" value="upload">
    </form>
    <?php
    if(isset($_POST['sb'])) {
    $file = $_FILES['pic']['tmp_name'];
    $newpath = "$file";
    move_uploaded_file($_FILES['pic']['tmp_name'], $path);
    echo $file;
    }
    ?>
    Code (markup):
     
    xcrox, Nov 20, 2008 IP
  10. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #10
    Also make sure that the script also have security option like user login authorization so that no one can access the script except u :)
     
    harrisunderwork, Nov 20, 2008 IP
  11. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #11
    Yeh, thanks guys, got my whole admin area sorted out for me now :D Wayyy easier than editing each text file and uploading pics myself lol, thanks!
     
    Magestry, Nov 20, 2008 IP
  12. vetrivel

    vetrivel Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    hi, what will happen if this sub folder contain one more folder with images.. will this work?
     
    vetrivel, Nov 20, 2008 IP
  13. AdultProfiles

    AdultProfiles Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    For what is used that $newpath variable?
     
    AdultProfiles, Nov 21, 2008 IP
  14. Supah!

    Supah! Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Be sure to secure it. Short < Secure.
     
    Supah!, Nov 22, 2008 IP