/    Sign up×
Community /Pin to ProfileBookmark

Hey guys,

I’m trying to create a form to upload a file to my webserver. That’s not a problem (I’ll gladly post my code I just didn’t want to clutter the post). However, once I submit this form I’m having trouble uploading the file successfully. I double checked and made sure my directories were CHMODed to 777. I get the following output:

[quote]

Temp: /tmp/phpAfAENc
Dest: folder/devotionals/To Do.txt

[/quote]
[quote]

Warning: move_uploaded_file(folder/devotionals/To Do.txt) [[URL=”http://domain.org/cms/function.move-uploaded-file”]function.move-uploaded-file[/URL]]: failed to open stream: No such file or directory in /home/.langston/domain/domain.org/cms/addEntry.php on line 23

[/quote]
[quote]

Warning: move_uploaded_file() [[URL=”http://domain.org/cms/function.move-uploaded-file”]function.move-uploaded-file[/URL]]: Unable to move ‘/tmp/phpAfAENc’ to ‘folder/devotionals/To Do.txt’ in /home/.langston/domain/domain.org/cms/addEntry.php on line 23

[/quote]

Now why on earth would there be a link to [url]http://domain.org/cms/function.move-uploaded-file[/url] ? I’m not sure how it even came up with that.

Here is my code:

[code=php]
$query=”SELECT path FROM FileUploadPaths WHERE nameOfTable='”. $_SESSION[‘tableName’] .”‘”;
$result=mysql_query($query);
$path;
while ($row = mysql_fetch_assoc($result))
$path=$row[‘path’];

$path = $path . basename( $_FILES[‘FileUploadRestricted’][‘name’]);
echo(“Temp: ” . $_FILES[‘FileUploadRestricted’][‘tmp_name’] . “<br>”);
echo(“Dest: ” . $path . “<br>”);

if(move_uploaded_file($_FILES[‘FileUploadRestricted’][‘tmp_name’], $path))
{
echo “The file “. basename( $_FILES[‘FileUploadRestricted’][‘name’]).
” has been uploaded”;
}
else
{
echo (“There was an error uploading the file, please try again!”);
}
[/code]

Any help is greatly appreciated!!!

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@bokehJan 14.2007 — Try writing to the destination directory with [I]fopen()[/I] or [I]file_put_contents()[/I].
Copy linkTweet thisAlerts:
@telconstar99authorJan 14.2007 — I successfully created a text file with the following script I found on the web. I think that verifies that writing to that directory is possible which I think is what you were trying to rule out.
[code=php]
$myFile = "folder/devotionals/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Boppern";
fwrite($fh, $stringData);
$stringData = "Tracy Tannern";
fwrite($fh, $stringData);
fclose($fh);
[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 15.2007 — The example you posted simply does what Bokeh suggested that you try out.

However, the example, though similar, is not quite the same as your original problem because you changed the file name (from "To Do.txt" to "TestFile.txt").

I think that if you were to try the example script againwith the original file name, you'll probably run into the same problem because it seems that your server is one of those who do not allow spaces in file names (Yahoo, for example).
Copy linkTweet thisAlerts:
@bokehJan 15.2007 — Well I guess the next step is to see if you can read the temp file.[code=php]echo file_get_contents($_FILES['FileUploadRestricted']['tmp_name']);[/code]
Copy linkTweet thisAlerts:
@telconstar99authorJan 15.2007 — Good idea bokeh. It appears to be doing that just fine. So it's at least uploading to the temp file correctly.
Copy linkTweet thisAlerts:
@NightShift58Jan 15.2007 — Have you tried writing a file with a space in the file name?
Copy linkTweet thisAlerts:
@telconstar99authorJan 15.2007 — Yes I have. Same result.
Copy linkTweet thisAlerts:
@Hendrick2Jan 15.2007 — Ok, I normally don't check the forums here, but yours was the first message up, and I remember how much trouble I had with FTP connection... drove me crazy. Especially when someone you know (who knows a lot more php then you do) told you it was impossible...

But, heres my script. Feel free to edit the die messages.

[code=php]// set up basic connection
$conn_id = ftp_connect($IP);

// login with username and password
$login_result = ftp_login($conn_id, $Username, $Password);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed! Contact your admin.";
exit;
} else {
echo "Uploading...";
}

//$_FILES['source_file']['tmp_name'] = "$destination_file";

//$destination_file=ftp_pwd($conn_id);

$destination_file="soontorename";


// upload the file

$upload = ftp_put($conn_id, "$destination_file", $source_file, FTP_BINARY);

// check upload status
if (!$upload) {
echo "<br>FTP upload has failed! Please contact your admin! <meta http-equiv="refresh" content="4;url=index.php">";
} else {
echo "<br>Upload! <meta http-equiv="refresh" content="0;url=index.php">";
}

// set the new name to unc
$unt = $_FILES['source_file']['name'];
//echo $unt;

$rename = ftp_rename ($conn_id, "soontorename", "$unt");

if ($rename)
print "File has been renamed!";
else
print "Renaming failed. Please contact your admin";


// rename the file back to origional name
//ftp_rename($conn_id,"soontorename",$unt);

// close the FTP stream
ftp_close($conn_id);[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 15.2007 — Essentially, the difference between move_uploaded_file() and rename() is that PHP first checks to ensure that the source file has been upload via PHP/HTTP.

Maybe something is foul at that level. You can try renaming the file to the new location.
Copy linkTweet thisAlerts:
@telconstar99authorJan 15.2007 — Well NightShift, rename works like a champ. When I was looking up how rename worked I saw that some people were using $_SERVER["DOCUMENT_ROOT"] in their destination path. So, b/c rename is working just fine with this path I tried it with move_uploaded_file() and voila! Now that's working just fine too! Something so simple. I wonder why the tutorials that I happened to read never said that it was required (in fact I don't recall them even mentioning it but I may be wrong on that account). I'm just glad we got this fixed, thanks for your help as well Bokeh!


Hendrick2: Thanks for the FTP script but it's probably a bit beyond what I wish to do. I just want to allow people to upload into a directory that is determined by their particular account privileges (I have my own content management system).
Copy linkTweet thisAlerts:
@NightShift58Jan 15.2007 — You're welcome...
×

Success!

Help @telconstar99 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...