/    Sign up×
Community /Pin to ProfileBookmark

How to get the filename

How can I get only the filename and not the complete path of a file when I use: <input name=”Image” type=”FILE”>

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 02.2004 — [font=arial]Hello Sam,

Can you show me the part of your script that is relevant to your question, please?[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Khalid_AliApr 02.2004 — go to php.net and read the docs for uploading files.

here is the short answer

when you upload file/files they are put ina global array(php ver>4 ) called $_FILES

from this array you can get the name of the file being uploaded by using something like this

$fileName = $_FILES['userfile']['name'];
Copy linkTweet thisAlerts:
@SamKookauthorApr 02.2004 — I did find something about $_FILES['userfile']['name'], but i jsut don't understant it. is userfile and name are var names or it's supposed to be my vars in the code?

Here is my code, the part in bold is where the filename is supposed to be.
<i>
</i>//Uploadage de l'image sur le serveur
$newfile = $myrow["NomTable"] . [B]"Filename_Here"[/B];
if(copy($_POST["Image"], $newfile))
{
print("&lt;h3&gt; UPLOAD SUCCESSFUL!&lt;/h3&gt;");
}
else
{
print("&lt;h3&gt;ERROR UPLOADING FILE!&lt;/h3&gt;");
}

mysql_query($QueryInsert,$db);

printf ("&lt;form name="Photos" method="post"
action="%s?ListeSection=%s&amp;Submit=False"&gt;",$_SERVER['PHP_SELF'],$_GET["ListeSection"]);
echo "&lt;p&gt;&lt;/p&gt;";
echo "&lt;p align="center"&gt;L'image a été enregistré avec succès.&lt;br /&gt;&lt;br /&gt;";
echo "&lt;input type="Submit" name="Submit" value="Retour"&gt;&lt;/p&gt;";
echo "&lt;/form&gt;";
}
else
Copy linkTweet thisAlerts:
@JonaApr 02.2004 — [font=arial]Use this[/font]

[code=php]
$newfile = $myrow["NomTable"] . $_FILES['Image']['name'];
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Khalid_AliApr 03.2004 — [i]Originally posted by SamKook [/i]

[B]I did find something about $_FILES['userfile']['name'], but i jsut don't understant it. is userfile and name are var names or it's supposed to be my vars in the code?



[/code] [/B]
[/QUOTE]


here is a bit more detail.

suppose you have an html file element

<input type="file" name="file"/>

now in the file array the first index value will be the the element name which in this case is "file"

now get the name

$fileName = $F_FILES['file']['name];

I hope this clears it..
Copy linkTweet thisAlerts:
@SamKookauthorApr 05.2004 — Ok, Now I understand it, but it sill don't work cause the var is empty.

I got an error Image not defined.

Any idea why.
Copy linkTweet thisAlerts:
@SamKookauthorApr 05.2004 — I had forgoten to put the encoding type in the form. Now it's working fine
Copy linkTweet thisAlerts:
@JonaApr 05.2004 — [font=arial]Contargulations. ? [/font]
×

Success!

Help @SamKook 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 5.6,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...