0

i have a forum for uploading pics or other files to a root folder but its not working online on my hosted domain at jnetscripts.com

it sais its working but no file is there, i chmod the folder root to allow writing and still does not work is there a mistake in below code or could it be my hosting settings?

code start (php)

<form enctype="multipart/form-data" action="upload.php" method="POST">
 Please choose a file: <input name="uploaded" type="file" /><br />
 <input type="submit" value="Upload" />
 </form> 



<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("root/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "root/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "root/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 
2
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by jamied_uk
Featured Replies
  • 1
    cereal 1,419   5 Years Ago

    Set an absolute path in the second argument of move_uploaded_files(), at the moment this is relative to the script. The same applies to file_exists(). You can use `$_SERVER['DOCUMENT_ROOT']`. Also use getimagesize() to ensure that uploaded file is really an image, at least do this check, the mime it self is … Read More

  • 1
    cereal 1,419   5 Years Ago

    Set a $path variable: $path = $_SERVER['DOCUMENT_ROOT'] . "root/"; Line 25: if (file_exists($path . $_FILES["file"]["name"])) Lines 31/32: move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]); In general: output $_SERVER['DOCUMENT_ROOT'] so you know if there is a trailing slash or not, if not then change $path variable to: $path = $_SERVER['DOCUMENT_ROOT'] . "/root/"; bye! Read More

  • 1
    cereal 1,419   5 Years Ago

    Yes, except for line 29 which is extra, that will give you an error, just remove it and check if the script is working, if not, then you have to change permissions, chmod 755 maybe enough. Read More

  • 1
    cereal 1,419   5 Years Ago

    Create a file with below code an upload it in your server: <pre> <?php $path = $_SERVER['DOCUMENT_ROOT'] . "/root/"; echo file_exists($path) ? 'directory exists ':'directory not found '; echo is_dir($path) ? 'it is a directory ':'it is not a directory '; echo is_writable($path) ? 'is writable ':'is not writable change … Read More

1

Set an absolute path in the second argument of move_uploaded_files(), at the moment this is relative to the script. The same applies to file_exists(). You can use $_SERVER['DOCUMENT_ROOT']. Also use getimagesize() to ensure that uploaded file is really an image, at least do this check, the mime it self is not reliable, since it can be spoofed..

Votes + Comments
what line number do i need to edit and what is the edit i should make and i will try it right now :d thanks for your reply
1

Set a $path variable:

$path = $_SERVER['DOCUMENT_ROOT'] . "root/";

Line 25:

if (file_exists($path . $_FILES["file"]["name"]))

Lines 31/32:

move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]);

In general: output $_SERVER['DOCUMENT_ROOT'] so you know if there is a trailing slash or not, if not then change $path variable to:

$path = $_SERVER['DOCUMENT_ROOT'] . "/root/";

bye!

Votes + Comments
is this correct?
Please choose a file:
0

@jamied_uk

please reply by using the appropriate box, the Vote & Comment is not for questions, for this same reason I cannot read your reply because is truncated.

0

is this how you meant i have edited and now looks like this

<form enctype="multipart/form-data" action="upload.php" method="POST">
    Please choose a file: <input name="uploaded" type="file" /><br />
    <input type="submit" value="Upload" />
    </form>
    <?php
    $path = $_SERVER['DOCUMENT_ROOT'] . "root/";
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists($path . $_FILES["file"]["name"]))
    {
    echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]);
    "root/" . $_FILES["file"]["name"]);
    echo "Stored in: " . "root/" . $_FILES["file"]["name"];
    }
    }
    }
    else
    {
    echo "Invalid file";
    }
    ?> 
1

Yes, except for line 29 which is extra, that will give you an error, just remove it and check if the script is working, if not, then you have to change permissions, chmod 755 maybe enough.

0

it did not work so i dont know why it would work on my localhost server just not on a domain hosted server :(

1

Create a file with below code an upload it in your server:

<pre>
<?php

    $path = $_SERVER['DOCUMENT_ROOT'] . "/root/";

    echo file_exists($path) ? 'directory exists ':'directory not found ';
    echo is_dir($path) ? 'it is a directory ':'it is not a directory ';
    echo is_writable($path) ? 'is writable ':'is not writable change permissions ';

?>
</pre>

This will tell you if there is a resource named root, if this is a file or a directory and if this is writable. Of course if something is wrong, for example a path issue o a slash you will get negative outputs. If you have doubts, paste results here so we can check it. Bye.

Edited by cereal

0

its a folder within another folder simply called root and it should be writable but wen i set chmod sometimes it returns error on reading folder and then it resets itself back to default chmod settings for the folder called root.

it is a gallery that only works when its called root but the folder can be anywhere and still work if this makes sense

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.