Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    $_FILES not working?

    Okay, I have made my form for an upload script I am making and the name of the file input is called 'file', so I assume this is what I am supposed to put when the form is submitted, I also have my form set to enctype="multipart/form-data" so I know that isn't the problem, which I have read is normally the culprit as to why it won't work.

    Anyway here is my code...

    PHP Code:
    $filename $_FILES["file"]["name"];
    $file_basename substr($filename0strripos($filename'.')); // strip extention
    $file_ext substr($filenamestrripos($filename'.')); // strip name
    $filesize $_FILES["file"]["size"];
    $title1 $_POST['title'];
    $title addslashes($title1);
    $link $_POST['link'];
    if ((
    $file_ext == ".png" || $file_ext == ".gif")  &&  ($filesize 200000)) {
    $newfilename md5($file_basename) . $file_ext;
    if (
    file_exists("images/" $newfilename)) {
    $error "Unexpected error, we're working on this! Try again!";
    } else {
    move_uploaded_file($_FILES["file"]["tmp_name"], "images/" $newfilename);
    $SQL "INSERT INTO `images` (`title`, `link`, `src`) VALUES ('$title', '$link', 'images/$newfilename')";
    $result mysql_query($SQL); 
    ...the only error I am getting is this; "Notice: Undefined index: file in C:\xampp\htdocs\tomaldy\pages\add_image.php on line 9" and then the exact same for line 12.

    I don't understand what is happening? I am not super-experienced in PHP so I understand if it's insanely obvious what's wrong.
    Do I have to $_POST my 'file' at all? I'm so confused.

    Any help is extremely appreciated as I'm sure you all know how much of a pain in the neck PHP can be sometimes... :P

    Once again, thanks for any help!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Post the code for the HTML form as well.

  • #3
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    <form enctype="multipart/form-data" method="post" action="pages/add_image.php" name="#menu">
    <input type="text" name="title"><br/><br/>
    <input type="text" name="link"><br/><br/>
    <li class="upload">
    <div class="fakeupload">
    <input type="text" name="fakeupload" id="fakeupload"/> 
    </div>
    <input type="file" name="file" id="realupload" class="realupload" onchange="this.form.fakeupload.value = this.value;" />
    </li><br/><br/>
    <input type="submit" name="go" style="width: 100px;" value="Add my image!"> - <input type="reset" name="reset" style="width: 100px;" value="Reset">
    </form>
    Here you go

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    80
    Thanks
    0
    Thanked 13 Times in 12 Posts
    How big is the file you are uploading? If it's bigger than the max size accepted by the server it'll fail. Try uploading something a couple of kilobytes in size to rule that out.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Remove the javascript from the value there and try again.

  • #6
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MarkR View Post
    How big is the file you are uploading? If it's bigger than the max size accepted by the server it'll fail. Try uploading something a couple of kilobytes in size to rule that out.
    I already had done it, but I tried again anyway; the error messages showed up again on the result page... :/

    The file was literally so small it was a dot pretty much.
    I even set it to check file types such as .GIF and .PNG (with capitals) but still got the error messages :/

  • #7
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Remove the javascript from the value there and try again.
    The problem still occured :/
    Thanks for trying though

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    In processing script add this to the top:
    PHP Code:
    printf('Post<pre>%s</pre>' PHP_EOLprint_r($_POSTtrue));
    printf('Files<pre>%s</pre>' PHP_EOLprint_r($_FILEStrue)); 
    View and drag the results of the browser html source and post that here.

  • #9
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    Post
    Array
    (
        [title] => dfdff
        [link] => ihihih
        [fakeupload] => C:\fakepath\delete_entry_button.gif
    )
    Files
    Array
    (
    )
    I suppose it could be because of the fake path?
    How would I go about changing that if that's the case?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Your files has not been populated.
    Disable your javascript completely in the browser and try again.

  • #11
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Could it be because I am using AJAX to submit the form?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by Remotive View Post
    Could it be because I am using AJAX to submit the form?
    Its looking that way to me. Have you disabled your javascript and run this directly from the html through to the PHP to verify?

  • #13
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I got more errors about moving the file and such, but it seemed to do the trick; is there any way I could make this work with the AJAX or not?
    Otherwise I would have to tell my users to upload it via a different server and then paste the image url instead of uploading it themselves?

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I don't understand what you mean about moving from an alternate server. Files works by moving from a local machine, not from a remote one.
    As for AJAX, post in the AJAX forum for that one. I can't see why it wouldn't be doable, but I don't know what needs to be changed in order to use it.

  • #15
    New Coder
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I mean they can type the image URL rather than it being hosted on my own web hosting.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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