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";
  }
?> 

Recommended Answers

All 10 Replies

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..

commented: 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 +0

ignore this line

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!

commented: is this correct? <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_R +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.

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";
    }
    ?> 

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.

ok i will try that tommor thanks for all your help everyone :D

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.