SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP upload file with id

    I am trying to upload a file with this code:

    $target_path = "../files/";
    $target_path = $target_path . basename($_FILES['pdf_upload']['name']);
    $_FILES['pdf_upload']['tmp_name'];

    if (move_uploaded_file($_FILES['pdf_upload']['tmp_name'], $target_path)){

    header("Location: properties.ssd");
    exit;
    }

    It works but I how can I append an id variable($id) to be uploaded with the id?

    Please help,

    Thanks,
    eddie

  2. #2
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean, how do you assign a unique id to the file uploaded?

    If the user needs to enter one, then you would provide a form field for it. You could then make that id part of the final file name, when you move it out of tmp. You would have to ensure that it was indeed unique and that it didn't contain invalid characters.

    Otherwise, just generate the id server side. It can be random numbers, a hashed value or something based on the data and time, combined with an IP or something else unique about the user. There are a lot of possibilities. If that doesn't help, you might have to be a little more specific.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    inside the <form>
    <input type="hidden" name="id" value="239746283" />

    And there is your id $_POST['id'];

  4. #4
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But after I grab that Id how can where do put it in the code?

    thanks,
    eddie

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well where do you want to put it?

  6. #6
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something so it can read:

    test.doc_id.pdf

    oh, its a pdf I'm uploading.

    thanks,
    eddie

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like:
    header("Location: properties.ssd?id=".$_POST['id']);
    OR store it in a database?

  8. #8
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this id variable called $lastid

    being used for my image uploads.

    but i don't know where to stick it in the pdf upload.

    when pdf is uploaded I want it to upload with that id variable.

    thnx,
    eddie

  9. #9
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for to mention. I didn't code this site. I am just trying to add a pdf upload feature.

    image upload code:
    if (is_uploaded_file($site_plan)) {
    $filename = imageSave("site_plan",$lastid,"_site_plan");

  10. #10
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops i meant forgot to mention.
    sorry

  11. #11
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you should add the lastid to the pdf's filename.

  12. #12
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this:

    $target_path = "../files/";
    $target_path = $target_path . basename($_FILES['pdf_upload'.$lastid.'']['name']);
    $_FILES['pdf_upload'.$lastid.'']['tmp_name'];

    if (move_uploaded_file($_FILES['pdf_upload'.$lastid.'']['tmp_name'], $target_path)){

    header("Location: properties.ssd");
    exit;

    but it did not upload the file.

    thnx,

    eddie

  13. #13
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    So you want...

    uploaded_file.pdf

    To be renamed to

    uploaded_file.1011.pdf

    Right?

    This only works for pdf but easy to change for diff files.

    PHP Code:
    $target_path $target_path '.' preg_replace('/\.pdf$/'$lastid$_FILES['pdf_upload']['name']).'.pdf'

  14. #14
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $target_path "../files/";
    $target_path $target_path.$lastid.'_'.basename($_FILES['pdf_upload']['name']);

    if (
    move_uploaded_file($_FILES['pdf_upload']['tmp_name'], $target_path)){

    header("Location: properties.ssd");
    exit; 

  15. #15
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let me try.

    so this works also if I want the file to name

    uploaded_file_1011.pdf

    thanx,
    eddie

  16. #16
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet.

    Many thanx!! I used:

    $target_path = "../files/";
    $target_path = $target_path.$lastid.'_'.basename($_FILES['pdf_upload']['name']);
    $_FILES['pdf_upload']['tmp_name'];

    if (move_uploaded_file($_FILES['pdf_upload']['tmp_name'], $target_path)){

    header("Location: properties.ssd");
    exit;

    accept now I want the id after the doc name.
    going to manipulate.

    Man this forum saved my butt.

    eddie

  17. #17
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_replace?

    Oh ok!

    let me try

  18. #18
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to append it after the original filename you can do a str_replace.
    PHP Code:
    $new_name str_replace('.pdf''_'.$lastid.'.pdf'basename($_FILES['pdf_upload']['name']));
    $target_path $target_path.$new_name
    After getting this to work you should look into securing the upload!

  19. #19
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet!

    Works!!

    Many Thanks

  20. #20
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your right. Anybody recommend a php book? LOL

  21. #21
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You should use preg_replace, not str_replace, because uploaded_file.pdf.pdf is a valid filename, but you don't want it to be renamed to uploaded_file.1011.1011.pdf, also using preg_replace allows you to add a generalize file extension should you want something other than pdfs uploadable, such as .doc or .txt

  22. #22
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OH, interesting. I have to read up on preg_replace.

    I just want pdfs uploaded and you're right I want it to me named

    uploaded_file_1234.pdf

    vs

    uploaded_file.12324.pdf

    thanks,
    eddie

  23. #23
    SitePoint Member
    Join Date
    Aug 2007
    Location
    El Paso, Texas
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry to be a bother.

    Now, I have to create a link to it so users can download it on display side. I was working on a admin side.

    thanks,
    eddie

  24. #24
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    You should use preg_replace, not str_replace, because uploaded_file.pdf.pdf is a valid filename, but you don't want it to be renamed to uploaded_file.1011.1011.pdf, also using preg_replace allows you to add a generalize file extension should you want something other than pdfs uploadable, such as .doc or .txt
    Good catch.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •