需要帮助将 echo 添加到 PHP 脚本的单行
对于 PHP 来说,我几乎是一个新手,并且有一个需要解决的问题,因为网站明天必须上线。
基本上我一直在使用一个名为“uploadify”的javascript上传脚本,它有一个上传文件夹的选项,该选项以以下形式添加到脚本中:
'folder' : '/songs/<?php echo $_SESSION["name"];?>',
我添加了php echo以从PHP登录返回用户名,因此它被添加到上传文件夹的路径(每个用户都有自己的上传子文件夹)。
使用新版本的 uploadify 脚本,文件夹选项已移至单独的 PHP 文件,现在的形式如下:
$targetFolder = '/songs/';
我需要找到一种将用户名变量添加到此行的方法。我尝试过以各种方式使用 echo,并用谷歌搜索,但它却难倒了我,尽管它可能很简单。
如果有人能让我知道如何构建这条线,我将非常感激。正如他们所说,时间至关重要......
谢谢,
尼克
,我想我应该包括所有涉及的 php,以防这说明为什么会话变量不可用于 uploadify.php。
首先在index.php页面的顶部有这个:
<?
require("log.php");
?>
它触发log.php:(
<?
session_name("MyLogin");
session_start();
if($_GET['action'] == "login") {
$conn = mysql_connect("","",""); // your MySQL connection data
$db = mysql_select_db(""); //put your database name in here
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM USERS WHERE login='$name'");
if(mysql_num_rows($q_user) == 1) {
$query = mysql_query("SELECT * FROM USERS WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION["name"] = $name;
header("Location: http://monthlymixup.com/index.php"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}
// if the session is not registered
if(session_is_registered("name") == false) {
header("Location: login.php");
}
?>
我已经从中删除了MySql信息)
然后调用login.php,它在顶部有这个php(后面是html形式):
<?
session_name("MyLogin");
session_start();
session_destroy();
if($_GET['login'] == "failed") {
print $_GET['cause'];
}
?>
其中包括 session_destroy();。我认为这可能会导致问题,但我删除了这一行,并且会话变量没有传递到 uploadify.php (为什么它可以用于 index.php 它在这里被破坏?)。 uploadify.php 目前看起来像这样:
<?php
session_start()
print_r($_SESSION);
$targetFolder = '/songs/' . $_SESSION['name']; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') .'/'. $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('m4a','mp3','flac','ogg'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
uploadify.php 在 jquery.uploadify.js 中被引用。我认为这些都是相关的:
uploader : 'uploadify.php',
upload_url : settings.uploader,
case 'uploader':
swfuploadify.setUploadURL(settingValue);
I am pretty much a new newbie when it comes to PHP, and have a problem that I need to solve, for a website that has to go live tomorrow.
Basically I have been using a javascript upload script called 'uploadify' which had an option for the upload folder which was added to the script in the form:
'folder' : '/songs/<?php echo $_SESSION["name"];?>',
I added the php echo to return the username from a PHP login, so it gets added to the path to the upload folder (each user has their own subfolder for uploading to).
With the new version of the uploadify script, the folder option has been moved to a separate PHP file where it is now in the form:
$targetFolder = '/songs/';
I need to find a way of adding the username variable to this line. I have tried using echo in various ways, and googled about, but it has stumped me, simple as it may be.
If anyone could let me know how I construct this line I'd be very grateful. Time is of the essence, as they say...
Thanks,
Nick
I thought I'd include all of the php involved in case this illuminates why the session var isn't available to uploadify.php.
First there is this at the top of the index.php page:
<?
require("log.php");
?>
Which triggers log.php:
<?
session_name("MyLogin");
session_start();
if($_GET['action'] == "login") {
$conn = mysql_connect("","",""); // your MySQL connection data
$db = mysql_select_db(""); //put your database name in here
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM USERS WHERE login='$name'");
if(mysql_num_rows($q_user) == 1) {
$query = mysql_query("SELECT * FROM USERS WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION["name"] = $name;
header("Location: http://monthlymixup.com/index.php"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}
// if the session is not registered
if(session_is_registered("name") == false) {
header("Location: login.php");
}
?>
(I have removed MySql info from this)
This then calls the login.php, which has this php at the top (followed by the html for the form):
<?
session_name("MyLogin");
session_start();
session_destroy();
if($_GET['login'] == "failed") {
print $_GET['cause'];
}
?>
which includes session_destroy();. Which I thought may be causing the problem, but I deleted this line, and the session var isn't passed to uploadify.php (and why would it be available to index.php it it is destroyed here?). uploadify.php currently looks like this:
<?php
session_start()
print_r($_SESSION);
$targetFolder = '/songs/' . $_SESSION['name']; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') .'/'. $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('m4a','mp3','flac','ogg'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
The uploadify.php is referred to in jquery.uploadify.js. I think these are all the lines that are relevant:
uploader : 'uploadify.php',
upload_url : settings.uploader,
case 'uploader':
swfuploadify.setUploadURL(settingValue);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
事情有这么简单吗?
Could it be this simple?
假设您仍然有会话变量可以使用......
Assuming you still have the session var to work with...
编辑:您的问题是
session_name
。如果您曾经使用它(通常没有必要),“您需要为每个请求调用 session_name() (并且在调用 session_start() 或 session_register() 之前)。”EDIT: Your issue is
session_name
. If you ever use that (which is not usually necessary), "you need to call session_name() for every request (and before session_start() or session_register() are called)."连接
只需使用连接....
如果您需要尾部斜杠(因为它之前没有它就可以工作,我认为您不需要),请使用:
当您在 PHP 脚本中时,将变量添加到另一个变量的字符串中是微不足道的 -使用“.”点运算符,您可以连接或连接字符串。
此外,
我还会检查会话名称变量是否存在,如果不存在,可能会取消或设置默认值。也许
这会主动为该文件找到一个新文件夹,并且不会覆盖现有文件夹。
链接
这里有一个简单的指南
PHP 手册
如上所述,另一种方法是使用双引号和“{} ”以达到相同的结果。您会发现,使用双引号而不是单引号可能会稍微慢一些,因为字符串解析器将检查您是否包含任何变量并将它们“扩展”为其值。两种语法都是可以接受的。
会话问题
这是我通常执行的传输会话 ID 的操作
看到脚本数据了吗?我基本上输出会话ID,然后将其传递到uploadify.php(我的示例不使用uploadify.php,它包含在一个类中,但它是同一件事)
Concatenation
Just use concatenation....
Should you need a trailing slash (since it was working before without It i dont think you do), use :
When you are within a PHP script, adding variables to the string of another variable is trivial - using the "." dot operator, you can concatenate or join strings.
Further
I would also check if the session name variable exists, and perhaps cancel out or set a default if it doesnt. perhaps
This will actively find a new folder for that file and it will not overwrite existing folders.
Links
A simple guide here
PHP Manual
As mentioned above, an alternative is to use double quotes and the "{}" to achieve the same result. What you'll find is that using double quotes over single quotes can be very marginally slower because the string parser will check to see if you've included any variables and 'expand' them out to their value. Both syntaxes are acceptable.
Session Problem
Here's what I normally do to transfer the session ID
See that script data? I basically output the session id and then pass that onto the uploadify.php (well my example doesn't use uploadify.php, that's contained in a class but it's the same thing)
您正在寻找连接运算符 ('.'):
You're looking for a concatenation operator ('.'):
只要用户名存储在会话变量中,请确保 $targetFolder 所在的脚本已启动会话 (session_start()) 并将该行更改为以下内容:
If $_SESSION['name'] = "foo “你的路径现在是/songs/foo/。
So long as the user name is being stored in a session variable make sure that whatever script $targetFolder resides in has the session started (session_start()) and change that line to the following:
If $_SESSION['name'] = "foo" your path would now be /songs/foo/.