www.webdeveloper.com
Page 22 of 23 FirstFirst ... 1220212223 LastLast
Results 316 to 330 of 340

Thread: How to: Upload images using PHP

  1. #316
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Post the bit of code that is building the query.

  2. #317
    Join Date
    Jul 2011
    Posts
    6
    Code:
    $query = "INSERT INTO properties ('location','description','image1','image2','image3','image4','image5','image6','image7','image8') VALUES ('$location','$description',";
    foreach($active_keys as $key)
    { 
    	$query .= "'$_FILES[$fieldname]['name'][$key]',"; 
    }
    $query .= ")";

  3. #318
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Echo this query and check it for syntax
    PHP Code:
    <?php

    # Assuming image1, etc accept null values you could try something like this... Not tested

    #set some variables
    $query_part_1 '';
    $query_part_2 '';
    $i 1;

    foreach(
    $active_keys as $key)

        
    $query_part_1 .= ",'image" $i++ . "'";
        
    $query_part_2 .= ",'" $_FILES[$fieldname]['name'][$key] . "'"
    }

    $query "INSERT INTO properties ('location','description'" $query_part_1 ") VALUES ('" $location "','" $description "'" $query_part_2 ")";

    ?>
    Last edited by bokeh; 07-08-2011 at 02:11 PM.

  4. #319
    Join Date
    Jul 2011
    Posts
    6
    Query was perfect! Thanks so much Bokeh, you are a life saver!

  5. #320
    Join Date
    Aug 2004
    Posts
    195

    onchange event replace submit?

    that did not help (I probably messed up) but I changed the name of the submit button to upload (When I use the onChange event, It doesn't seem to find the $_POST['upload'] variable
    http://fixmysite.us/asp/providers/test1.php
    I have this if statement in upload.processor.php
    PHP Code:
    // check the upload form was actually submitted else print the form
    isset($_POST['upload'])
        or 
    error('the upload form is neaded'$uploadForm); 
    But seems to see it if the submit button is clicked here
    http://fixmysite.us/asp/providers/test.php
    Is that right? why doesnt the event act as if the submit button is clicked?

    Let me explain what I'm trying to do as you might have a better way for me to do this...
    I'm trying to upload an image the moment the onchange event is triggered so that the image in thumbnail size appears so that the upload is confirmed to the stupid, also how can this be done without refreshing the page as ill have another form on the page I dont want to reset?

    Thanks a lot...

  6. #321
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by lukeurtnowski View Post
    I'm trying to upload an image the moment the onchange event is triggered so that the image in thumbnail size appears so that the upload is confirmed to the stupid, also how can this be done without refreshing the page as ill have another form on the page I dont want to reset?

    Thanks a lot...
    That's a Javascript issue. You could do it like vBulletin, i.e. do the upload in a separate window.

  7. #322
    Join Date
    May 2012
    Posts
    5
    Hi, I was wondering if you could help me please? I've created a very basic CMS and would like to allow the site's administrator to upload some pre-named images eg. image1.jpg, image2.jpg. They then need to be able to add them to pages using a form. I've set all of this up and they can add text with no problems. I would like them to be able to insert images into the pages by using <img src="image1.jpg" /> etc but this won't work due to the long string of numbers added to the image, as referred to in post #68. From reading further posts I understand that this relates to the date and time of upload and comes from the following lines of code:

    // make a unique filename for the uploaded file and check it is
    // not taken... if it is keep trying until we find a vacant one
    $now = time();
    while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
    {
    $now++;
    }

    // now let's move the file to its final and allocate it with the new filename
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $uploadForm);

    Now, I am a complete numpty at php and was wondering if you could please tell me EXACTLY what code I need to remove (or add) to fix the issue as I can't figure this out myself? I have tried and it's probably very simple and obvious when you know what you're doing but I can't get it to work.
    Many thanks

  8. #323
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by Beksters View Post
    PHP Code:
    $now time();
    while(
    file_exists($uploadFilename $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
    {
        
    $now++;
    }

    // now let's move the file to its final and allocate it with the new filename
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
        or 
    error('receiving directory insuffiecient permission'$uploadForm); 
    That code was just an example of one way to name a file so there weren't collisions. If you want to call a file something specific just write it into your code.

    PHP Code:
    $uploadFilename 'yourFileName.jpg';
    move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename); 

  9. #324
    Join Date
    May 2012
    Posts
    5
    Thanks for the response, but it's not quite what I meant. Sorry, I probably didn't explain it very well. I don't want to rename the file with the php code, I just want it to upload with exactly the same name it has when the user browses to it and selects it for upload, whatever that name will be. I don't wish to change that name, I just want the user to know what name to refer to when they enter <img src="...... .jpg" /> to add the image in a page.
    Using your original method, which adds a long string of numbers before the image name, there is no way of knowing how to reference that image. I just want the user to be able to upload any files they wish and keep their original names. Is this possible?
    Thanks.

  10. #325
    Join Date
    May 2012
    Posts
    5
    I forgot to mention that they won't be able to access the folder on the server to find the new file name, which is why it's such an issue to alter the name.

  11. #326
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by Beksters View Post
    I forgot to mention that they won't be able to access the folder on the server to find the new file name, which is why it's such an issue to alter the name.
    Allowing people to decide the file name is a major security risk.

  12. #327
    Join Date
    May 2012
    Posts
    5

    Smile

    It's okay, I just need to do this for a college project and the site won't go live so security is not going to be an issue in this case. I just need to be able to show that a user can upload an image to a folder and insert it into a page. The only way I can do it with the CMS I have is if the file name remains the same after upload so that the administrator can easily make a reference to it without having to access the files on the server. I'd really appreciate it if you could show me how to do this please? I won't be hand-coding any websites with PHP in the future, I'll use something like Wordpress or Joomla as I find PHP too complicated.
    Cheers!

  13. #328
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Nothing complicated about PHP.

    PHP Code:
    $uploadFilename $_FILES[$fieldname]['name']; 

  14. #329
    Join Date
    May 2012
    Posts
    5
    Thanks for your help, that works beautifully

  15. #330
    Join Date
    Jun 2012
    Posts
    4

    Red face help with email notification

    You are amazing and I have successfully set up using your codes etc but don' t know anything about coding with php.

    When I receive the email notification I need the following details to be in the email

    1. The name of the image the user uploaded - (with the renaming)
    2. Preferably the url of the image
    3. the details of the form that the person had filled out eg: name, address, email address etc. I'm sort of stuck in how to add those details to the actual html form so the php can read those fields
    4. would also love to email the person automatically with confirmation

    I really hope you can help me. I haven't changed the code at all, it's exactly as on the sticky so I won't bother posting it here. I have googled this question and read through all 22 pages but still not sure how to get what I need. thanks

    Jody

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

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



Recent Articles